可以用询问者创建分支问题吗?

It is possible to create branching questions with inquirer?

const employeeQuestion = [
    {
        type: "list",
        name: "employeeTitle",
        message: "What's the employee's title",
        choices: ["Engineer", "Intern"]

    },

    //when role is engineer is true, ask this question
    {
        when: input => {
            return input.role == "Engineer"
        },
        type: "input",
        name: "github",
        message: "Enter your github username:",
    },

    //when role is intern is true, ask this question
    {
        when: input => {
            return input.role == "Intern"
        },
        type: "input",
        name: "school",
        message: "What's the school you enrolled in ?",
    },

]

我想利用询问者的 when 方法,所以是否询问用户 github 或学校的问题取决于员工职位的答案.但是当我在命令行上 运行 节点时,要求 github / school 的问题从未出现过。请问是我方法用错了还是有其他方法

询问者的when方法对于这种情况绝对是正确的!

可能有两个原因:

  1. 您正在使用 input.role 但从未在答案哈希中定义 role 值。您需要参考前面问题中的 name 值,在本例中为 input.employeeTitle.

  2. 如果原因 1 不能解决问题,请尝试扩展您的功能。 需要将答案散列作为输入时,然后对其应用条件并显式 return 一个布尔值。例如

    {
         type: "input",
         name: "github",
         message: "Enter your github username:",
         when: (answers) => {
             if (answers.employeeTitle === "Engineer") {
                 return true;
             }
    }