1

我正在使用 Inquirer.js 编写帐户创建者,并且仅在用户要求此类保护时才想询问用户的电子邮件和密码。它是这样的:

inquirer.prompt([
        {type:'input', name:'username', message:'Choose an username:'}, 
        {type:'confirm', name:'protect_ask', message:'Do you want to password-protect your game list?'},
        {type:'input', name:'email', message:'E-mail (for password recovering):', when:function(answers){return answers.protect_ask}},
        {type:'password', name:'password', message:'Password:', when:function(answers){return answers.protect_ask}},
        {type:'password', name:'confirm_password', message:'Confirm you password:', when:function(answers){return answers.protect_ask}}
    ]

如您所见,我运行了完全相同的when语句三次,因为它验证是否选择了密码保护。我想知道是否有更智能、更实用的方法来执行此操作,即在不重复when函数的情况下运行此验证。

也许嵌套三个问题就可以了,但是如何在不破坏提示流程的情况下这样做呢?

4

0 回答 0