gpt4 book ai didi

javascript - 在生成的模板中使用 yeoman-generator 变量

转载 作者:行者123 更新时间:2023-11-30 15:18:11 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com