gpt4 book ai didi

javascript - 在 InquirerJS 中重复一组问题

转载 作者:行者123 更新时间:2023-11-30 20:43:54 26 4
gpt4 key购买 nike

有没有办法重新设置问题或让某个答案将问题引向另一个先前的问题?

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
},
]

最佳答案

这似乎有点老套,但我相信您必须在您的应用程序逻辑中处理它。例如:

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');
}
})

更新:

仔细查看文档后,似乎“何时”才是正确的解决方案。

when: (Function, Boolean) Receive the current user answers hash and should return true or false depending on whether or not this question should be asked. The value can also be a simple boolean.

关于javascript - 在 InquirerJS 中重复一组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48944397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com