yeoman - 在子生成器中使用 composeWith 选项

标签 yeoman yeoman-generator

我正在组装一个 Yeoman Generator,但在理解 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;
  }
}

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

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

最佳答案

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

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更多细节。

关于yeoman - 在子生成器中使用 composeWith 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29808555/

相关文章:

gruntjs - 使用各种浏览器运行 grunt 服务器

node.js - 安装生成器平均种子时出错

angularjs - 最新的 Angular 生成器在我的 macbook pro 上抛出了很多错误

node.js - Yeoman Generator 无法读取 1.1 版本中未定义的属性 Base

angularjs - Velocity 已经加载 - Materialise yeoman

node.js - Yeoman 生成器发布到 npm 后不包含子生成器

javascript - 使用 RequireJS 时,我应该将脚本放在 Yeoman 项目的什么位置?

javascript - 如何将 angularJS 与 Yeoman 的 webapp-generator 一起使用

node.js - 在 Yeoman 中复制除一个子文件夹之外的文件夹

javascript - 如何读取刚刚使用 yeoman 写入的文件?