【问题标题】:how to pass a array of values to the template in the yeoman generator如何将一组值传递给 yeoman 生成器中的模板
【发布时间】:2020-06-04 02:14:00
【问题描述】:

我正在使用 yeoman-generator,我需要将一个值数组传递给模板文件。 例如,我想将一组文本值传递给模板。

  install() {
      async app() {
         let array = ['abc','cde','efg'];
           this.fs.copyTpl(
            this.templatePath('base','/abc.ts'),
            this.destinationPath('example/abc.ts'),
            {'array':array}
          );         
      } // app 
  }// install 

在 .ts 文件中,我定义了一个接收传递数组的变量。

public options =  <%= array %> ;

我的预期结果是

public options =  ['abc','cde','efg'] ;

然而结果输出是

 public options =  abc,cde,efg ;

我将如何解决这个问题?

【问题讨论】:

    标签: yeoman yeoman-generator


    【解决方案1】:

    在 index.js 中,数组变量显示为

    让数组 = ['abc','cde','efg'];

    然后在模板文件中,如果要显示 ['abc','cde','efg'] 而不是 abc,cde,efg, 您只需要在模板文件中编写代码

    [ '${value}').join(',') %>]

    【讨论】:

    • 您能否为此添加更多上下文?
    猜你喜欢
    • 2014-06-26
    • 2022-08-11
    • 1970-01-01
    • 2011-08-09
    • 2019-05-27
    • 2017-09-20
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    相关资源
    最近更新 更多