有没有办法重置问题或有一个确定的答案将问题直接指向另一个先前的问题?
var questions = [{
{
name: 'morefood',
message: 'Do you want more food?',
type: 'list',
choices: [ 'Yes', 'No'],
},{
name: 'choiceoffood',
message: 'Which food do you want more of?',
type: 'list',
choices: [ 'Hamburgers', 'Fries', 'Hotdogs']
when: function(answers) {
return answers.morefood === 'Yes';
}
}, {
name: 'quantityoffood',
message: 'How much more do you want?',
type: 'input',
when: function(answers) {
return answers.quantityoffood === 'Yes';
}
},{
name: 'confirmfood',
message: 'Do you still want more food?',
type: 'list',
choices: [ 'Yes', 'No'], <=========== if yes then goes back to choiceoffood
},
]发布于 2018-05-02 08:36:50
这看起来有点老生常谈,但我相信你必须在你的应用程序逻辑中处理这一点。例如:
const questionA = {
type: 'input',
message: 'Do you like fruit?',
name: 'questionA',
}
const questionB = {
type: 'input',
message: 'What is your favorite fruit?',
name: 'questionB',
}
const questionC = {
type: 'input',
message: 'what is your favorite candy?',
name: 'questionC',
}
inquirer
.prompt(questionA)
.then(answers => {
if (answers.questionA === 'yes') {
return inquirer.prompt(questionB)
} else {
return inquirer.prompt(questionC)
}
})
.then(answers => {
if (answers.questionB) {
return console.log(answers.questionB, 'is a great fruit');
}
if (answers.questionC) {
return console.log(answers.questionC, 'is a great candy');
}
})更新:
在看了更多的文档之后,看起来“当”是解决这个问题的正确方法。
函数何时:(,Boolean)接收当前用户的答案散列,并根据是否应询问此问题返回true或false。该值也可以是一个简单的布尔值。
https://stackoverflow.com/questions/48944397
复制相似问题