首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在InquirerJS中重复一组问题

在InquirerJS中重复一组问题
EN

Stack Overflow用户
提问于 2018-02-23 17:04:49
回答 1查看 113关注 0票数 1

有没有办法重置问题或有一个确定的答案将问题直接指向另一个先前的问题?

代码语言:javascript
复制
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
}, 
]
EN

回答 1

Stack Overflow用户

发布于 2018-05-02 08:36:50

这看起来有点老生常谈,但我相信你必须在你的应用程序逻辑中处理这一点。例如:

代码语言:javascript
复制
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。该值也可以是一个简单的布尔值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48944397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档