简体   繁体   English

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

[英]Using yeoman-generator variable inside generated template

I am generating an angularJS project with yeoman. 我正在用yeoman生成一个angularJS项目。 I call the generator with an argument and I get it from my main generator's script (index.js) by doing: 我用一个参数调用生成器,然后从主生成器的脚本(index.js)中获取它:

this.argument('name', { type: String });

I can use this parameter inside this same index.js by using: 我可以在同一index.js中使用以下参数:

this.options.name

Question: I want to use this same this.options.name inside my template, so that i can give its value to a variable. 问题:我想在模板中使用相同的this.options.name,以便将其值赋给变量。 How can I do it? 我该怎么做?

To access a yeoman variable from Node.js, I did the following: 要从Node.js访问yeoman变量,我做了以下工作:

I renamed the file connections.js from sails, renamed it to _connections.js and placed it at the same level than sails folder inside the folder "templates" from yeoman's generator. 我从Sails重命名了connections.js文件,将其重命名为_connections.js,并将其放置在与yeoman生成器的“ templates”文件夹内的sails文件夹相同的级别。 Leaving it like this: 像这样离开:

> yeomanGeneratorFolder 
>   |-app   
>      |-index.js   
>      |-templates
>        |-sails
>        |-_connections.js
>        |- ....

On the Generator's index.js. 在Generator的index.js上。 I created a function to write on a js file the data I needed. 我创建了一个函数,可以在js文件中写入所需的数据。 I sent a JSON object inside the copyTpl() function which contains the data I will need in the new file: 我在copyTpl()函数内部发送了一个JSON对象,其中包含新文件中所需的数据:

connections() {
      this.fs.copyTpl(this.templatePath('_connections.js'), this.destinationPath('./config/connections.js'),
      {
        dbName: this.item.dbName
      });
    }

In _connections.js I wrote: 在_connections.js中,我写道:

database: '<%=dbName%>' //optional

Notice dbName is the key I gave to the JSON I sent inside the copyTpl() function in index.js. 注意, dbName是我提供给在index.js中的copyTpl()函数内部发送的JSON的密钥。

Define an angular constant and set it to this value. 定义一个角度常数并将其设置为该值。 Then inside the controller associated to this view, just inject this constant. 然后,在与该视图关联的控制器内部,只需注入此常数。

myApp.constant('YEOMAN_NAME', MYVALUE);

Then just inject your constant inside the controller associated to your view, associate this constant to a scope variable and use it inside your template. 然后,只需将常量注入与视图关联的控制器中,将此常量关联到作用域变量,然后在模板中使用它即可。

Now the "hardest" part is how to get this value inside the angular environment. 现在,“最困难的”部分是如何在角度环境中获取此值。

I don't know what is your index.js, I suppose that is only the main script associated to your page. 我不知道您的index.js是什么,我想那只是与您的页面相关的主要脚本。 In this case, if you do inside the main script something like 在这种情况下,如果您在主脚本中执行类似

this.myVariable = 'myValue';

You are actually creating a new attribute to the window object (if you are not doing that inside some function) 您实际上是在为窗口对象创建一个新属性(如果您未在某些函数中执行此操作)

So what you could do is to associate this variable to a window parameter, and try to be most specific as possible so you are not on risk to override something else in the window, like: 因此,您可以做的就是将此变量与窗口参数相关联,并尝试使其尽可能具体,这样您就不会冒着覆盖窗口中其他内容的风险,例如:

window.myYeomanName = this.options.name;

Then you can define your constant inside angular everywhere just doing: 然后,您可以在任何地方定义常量的内部角度,只需执行以下操作:

myApp.constant('YEOMAN_NAME', window.myYeomanName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM