【问题标题】:Use composeWith options in sub-generator在子生成器中使用 composeWith 选项
【发布时间】:2015-04-22 20:51:29
【问题描述】:

我正在组装一个 Yeoman 生成器,但无法理解 composeWith() 的 options 参数是如何工作的。我的目标是将用户输入从主生成器的提示传递给子生成器,我认为选项是这样做的方式。

我的主生成器的提示看起来像这样:

prompting: function () {
    var done = this.async();

    var prompts = [
      {
        type    : 'input',
        name    : 'name',
        message : 'What is the name of your project?',
        default : this.appname // Default to current folder name
      }
    ];

    this.prompt(prompts, function (answers) {

        this.composeWith('app:subgenerator', {
          options: {
            name: answers.name;
          }
        },
        {
          local: require.resolve('generator-angular/app')
        });
        done();
    }.bind(this)
  )
}

我尝试在我的子生成器中使用构造函数中的参数设置一个局部变量(因为我假设那是选项所在的位置),如下所示:

module.exports = generators.Base.extend({
  constructor: function () {
    generators.Base.apply(this, arguments);
    this.foo = arguments.options.name;
  }
}

但这没有用。我尝试控制台记录 arguments 变量,它显示 options 是一个对象,但它似乎是空的。

这是我可以通过生成器将用户输入传递给另一个生成器的方式,还是有其他方法可以做到这一点?

【问题讨论】:

    标签: yeoman yeoman-generator


    【解决方案1】:

    您的子生成器需要像编写期望选项的普通生成器一样编写。

    module.exports = generators.Base.extend({
      constructor: function () {
        generators.Base.apply(this, arguments);
    
        this.option('name');
      },
    
      initializing: function () {
        console.log(this.options.name)
      }
    }
    

    有关详细信息,请参阅user interaction documentation

    【讨论】:

    • 是的,成功了。不太了解文档中的选项是如何工作的。谢谢!
    • @JoshVickerson 随时发送 PR 以澄清我们的文档,我们将不胜感激!
    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    相关资源
    最近更新 更多