我正在使用 yeoman 生成一个 angularJS 项目。我使用参数调用生成器,并通过执行以下操作从主生成器的脚本 (index.js) 中获取它:
this.argument('name', { type: String });
我可以通过以下方式在同一个 index.js 中使用此参数:
this.options.name
问题:我想在模板中使用相同的 this.options.name ,以便我可以将其值赋予变量。我该怎么做?
最佳答案
要从 Node.js 访问 yeoman 变量,我执行了以下操作:
我将 sails 中的文件connections.js 重命名为 _connections.js 并将其放置在与 yeoman 生成器的“templates”文件夹中的 sails 文件夹相同的级别。保持这样:
> yeomanGeneratorFolder
> |-app
> |-index.js
> |-templates
> |-sails
> |-_connections.js
> |- ....
在生成器的index.js上。我创建了一个函数来在 js 文件上写入我需要的数据。我在 copyTpl() 函数内发送了一个 JSON 对象,其中包含新文件中需要的数据:
connections() {
this.fs.copyTpl(this.templatePath('_connections.js'), this.destinationPath('./config/connections.js'),
{
dbName: this.item.dbName
});
}
在 _connections.js 中我写道:
database: '<%=dbName%>' //optional
注意dbName是我为在index.js中的copyTpl()函数内发送的JSON提供的 key 。
关于javascript - 在生成的模板中使用 yeoman-generator 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44183321/