[英]Can I define an option in a Yeoman subgenerator, and then reference that option from the base generator?
I have an EJS template file that is located in the base generator, but needs to be able to read options from both the base generator and the subgenerator. 我有一个EJS模板文件,该文件位于基本生成器中,但需要能够从基本生成器和子生成器中读取选项。
module.exports = generators.Base.extend({
constructor: function() {
generators.Base.apply(this, arguments);
this.option('option1');
},
initializing: function() {
this.composeWith('base:sub-gen', this.options, {
local: require.resolve('../sub-gen'),
link: 'strong'
});
}
});
module.exports = generators.Base.extend({
constructor: function() {
generators.Base.apply(this, arguments);
this.option('option2');
},
});
Base generator option: <%=this.options.option1 %>
Sub-generator option: <%=this.options.option2 %>
Is there any way to reference the sub-generator options from my ejs template? 有什么方法可以从我的ejs模板中引用子生成器选项吗? Barring that, how else can I make sure that my base generator and my sub-generator all have access to the same list of options?
除非如此,否则我如何才能确保我的基本生成器和子生成器都可以访问相同的选项列表? Maybe it's possible using
.yo-rc.json
? 也许可以使用
.yo-rc.json
吗?
After working on it some more, I found a solution: 经过更多努力之后,我找到了解决方案:
configuring
step, but read them in the default
step. configuring
步骤中设置configuring
,但在default
步骤中读取它们。 this.config.get('key')
or this.config.getAll()
, save them as a property of the generator you want to use them in: this.imports = this.config.get('option-name')
. this.config.get('key')
或this.config.getAll()
阅读配置后,将它们另存为要在其中使用它们的生成器的属性: this.imports = this.config.get('option-name')
。 module.exports = generators.Base.extend({
constructor: function() {
generators.Base.apply(this, arguments);
this.option('option1');
},
initializing: function() {
this.composeWith('base:sub-gen', this.options, {
local: require.resolve('../sub-gen'),
link: 'strong'
});
},
configuring: function () {
let configs = {
option1: option1
};
this.config.set(configs);
},
default: function() {
let config = this.config.getAll()
this.imports = this.config.get('subGenOptions');
},
});
module.exports = generators.Base.extend({
constructor: function() {
generators.Base.apply(this, arguments);
this.option('option2');
},
configuring: function() {
let configs = {
subGenOptions: {
option2: this.options.option2
}
}
this.config.set(configs)
},
});
Base generator option: <%=options.option1 %>
Sub-generator option: <%=imports.option2 %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.