0
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 或学校的问题取决于员工标题的答案。但是当我在命令行上运行 node 时,从未出现过询问 github / school 的问题。我想知道我是否使用了错误的方法或是否有其他选择。

4

1 回答 1

1

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

有两个可能的原因:

  1. 您正在使用input.role但从未role在答案哈希中定义值。您需要参考前面问题中的名称值,在这种情况下,input.employeeTitle.

  2. 如果原因 1 没有解决它,请尝试扩展您的功能。需要将答案哈希作为输入时,然后对其应用条件并显式返回布尔值。前任。

    {
         type: "input",
         name: "github",
         message: "Enter your github username:",
         when: (answers) => {
             if (answers.employeeTitle === "Engineer") {
                 return true;
             }
    }
    
于 2020-10-14T19:16:49.693 回答