【问题标题】:Yeoman repeat prompt based on user inputYeoman 根据用户输入重复提示
【发布时间】:2019-07-19 19:30:20
【问题描述】:

比如我想问用户面包的种类:

        {
            type: 'list',
            name: 'breadType',
            message: `What type of bread do you want?`,
            choices: response => {
                const breadOptions = [
                    {
                        value: 'wheat',
                        name: 'Wheat Bread'
                    },
                    {
                        value: 'white',
                        name: 'White Bread'
                    }
                ];
                return breadOptions;
            },
            default: 0
        }

然后我会根据他们想要的浇头数量来要求浇头:

    {
        when: response => response.breadType,
        type: 'input',
        name: 'numberOfToppings',
        message: 'Please enter how many toppings you want: '
    }

无论用户输入多少次浇头,我如何提示?:

 {
        when: response => response.numberOfToppings,
        type: 'input',
        name: 'toppingChoices',
        message: 'Please provide your topping(s): '
 }

样本输入

? Please enter how many toppings you want: 4
? Please provide your topping(s):cheese
? Please provide your topping(s):onions
? Please provide your topping(s):pickles
? Please provide your topping(s):tomatoes

我不熟悉 yeoman 语法,请帮忙。

【问题讨论】:

    标签: generator yeoman prompt


    【解决方案1】:

    我做了一个简单的生成器来解决你的问题:

    const Generator = require('yeoman-generator');
    let toppings = [];
    
    module.exports = class extends Generator {
        async prompting() {
            const answers = await this.prompt([
                {
                    type: 'input',
                    name: 'numberOfToppings',
                    message: 'Please enter how many toppings you want: ',
                }
            ]);
    
            var length = answers.numberOfToppings;
            for(var i = 0; i < length; i++) {
                const answers2 = await this.prompt([
                    {
                        type: 'input',
                        name: 'toppings',
                        message: 'Please provide your topping(s):',
                    }
                ]);
                toppings.push(answers2.toppings);
            }
            console.log('Array: ' + toppings);
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      相关资源
      最近更新 更多