javascript - 可以用询问者创建分支问题吗?
问题描述
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 的问题。我想知道我是否使用了错误的方法或是否有其他选择。
解决方案
对于这种情况,询问者的when方法绝对是正确的方法!
有两个可能的原因:
您正在使用
input.role
但从未role
在答案哈希中定义值。您需要参考前面问题中的名称值,在这种情况下,input.employeeTitle
.如果原因 1 没有解决它,请尝试扩展您的功能。当需要将答案哈希作为输入时,然后对其应用条件并显式返回布尔值。前任。
{ type: "input", name: "github", message: "Enter your github username:", when: (answers) => { if (answers.employeeTitle === "Engineer") { return true; } }
推荐阅读
- c# - ASP如何在Resx文件的构建类型为内容时读取资源?
- c - 如果两个数字共享符号而没有 if 语句,如何返回 1?
- ios - 如何创建浮点字典数组并对其进行排序
- c# - 将 switch 语句重构为枚举
- mysql - 如何在 Macbook 上使用 Mysql 和 Visual Studio
- javascript - 打字稿将日期转换为字符串,如“20140127T224000Z”?
- javascript - React Native 0.56:bundle 不生成 .meta 文件 - 它仍然需要吗?
- visual-studio - Visual Studio 2015 智能感知添加了不必要的命名空间前缀,然后告诉我它们是不必要的
- android - 使用 Retrofit2 上传图片(PATCH 请求)
- react-router-dom - styled-components:如何防止道具被发送到链接组件?