【问题标题】:How is it possible in Node js to display list of options via console log and ask user to input a option from the displayed list via console log如何在 Node js 中通过控制台日志显示选项列表并要求用户通过控制台日志从显示的列表中输入一个选项
【发布时间】:2022-01-02 03:21:02
【问题描述】:

如何在 Node js 中通过控制台日志显示选项列表并要求用户通过控制台日志从显示的列表中输入一个选项

预期行为 user@desktop:~$ node test.js

  1. 选项 1
  2. 选项 2
  3. 选项 3

选择选项继续:3 您已选择选项 3

【问题讨论】:

标签: javascript node.js npm user-input console.log


【解决方案1】:

想出一个使用查询模块的方法

const inquirer = require('inquirer');
function main() {
inquirer
  .prompt([
    {
      type: 'list',
      name: 'Options',
      message: 'Select an Item from below list',
      choices: ['alligator', 'crocodile', 'Exit'],
    },
  ])
  .then(answers => {
        if (answers.Options == "Exit"){
                process.exit()
        } else {
                console.info('Answer:', answers.Options);
        }
  });
}
if (require.main === module) {
  main();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多