Yeoman生成器提示

5
我正在构建一个yeoman生成器,需要在回答提示时分配额外的值/答案。
我已经找到了一种方法来提示用户进行另一个问题,但我需要在后台自动分配预定义的答案 - 这样用户就不会看到它发生,也不会询问用户另一个问题。 下面是一个示例。此外,我需要对12个以上的值执行此操作,因此“when”命令不是理想的选择,因为我必须12次以上使用when语句。
   this.prompt([{
      type: 'list',
      name: 'redWhite',
      message: 'what colour',
      choices: ['red', 'white', 'blue', 'black', 'green', 'yellow', 'purple', 'cyan', 'magenta', 'brown']
    }, {
      when: 'redWhite.red',
      type: 'confirm',
      name: 'blue',
      message: 'Red is nice, but how about blue instead?'
    }, 

  /*So instead of prompting user again, just need to assign a predefined value here
   , {
      when: 'redWhite.red',
      answer: redFooBar
    }, */

    {
      when: 'redWhite.white',
      type: 'confirm',
      name: 'green',
      message: 'White is nice, but how about green instead?'
    }, {
      name: 'otherColors',
      message: 'What other colors do you like?'
    }], function (answer) {
      // answer = {
      //   redWhite: 'red',
      //   blue: false,
      //   green: false,
      //   otherColors: 'pink, purple-ish'
      // };
    }); 
1个回答

7
你不需要在这里使用when函数,因为它只是提示用户回答另一个问题。
你可以在提示回调函数中简单地使用数组来获取一个提示回答的多个值,例如:
//prompt user to answer questions
this.prompt([{
        type: 'list',
        name: "fruit",
        message: "What is your favourite fruit",
        choices: [{
            name: 'Apple',
            value: ['apple', 'Apple Juice', 'Apple Pie' ]
        }, {
            name: 'Banana',
            value: ['banana','Banana Juice','Banana Bread']
        }, {
            name: 'Oranges',
            value: ['oranges','Orange Juice','Orange Pudding']
        }]
    }]);

//confirming the prompts and storing answers - pull required value from array number
this.prompt(prompts, function(answers) {
  this.fruitChoice = answers.fruit[0];
  this.fruitDrink = answers.fruit[1];
  this.fruitDessert = answers.fruit[2];
}

不相关的问题:您能将数组传递到模板中并在模板中进行迭代吗?谢谢。 - Polynomial Proton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接