如何在yeoman生成器中正确地进行条件提示

How to properly do a conditional prompt in yeoman generator

本文关键字:正确地 条件 提示 yeoman      更新时间:2023-10-02

我要做的是,只有当一个字段在提示中设置为true时,才询问一个问题。

this.prompt(prompts, function (props) {
  this.isShared = props.isShared;
  this.componentName = props.componentName;
  // To access props later use this.props.someAnswer;
  if (this.isShared)
  {
    prompts = [{
      name: 'inPack',
      message: 'Yo dawg, in which pack is your component ? (you can just press enter if it''s not in a pack)',
      default: ''
    }]
    this.prompt(prompts, function (props) {
      this.inPack = props.inPack;
    }.bind(this));
    done();
  }
  else
    done();
}.bind(this));

问题是"writing"函数是在if之前调用的(不管if如何工作),所以我想知道如何正确地执行它,因为我想这不是

的好方法

Well Yeoman只是JavaScript,所以如果您正确处理异步订单,任何if/else语句都可以工作。

话虽如此,如果您依赖于问题的when属性,那么您将消除所有异步开销。看见https://github.com/SBoudrias/Inquirer.js#question