我正在组装一个 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/