簡體   English   中英

如何在 Node js 中通過控制台日志顯示選項列表並要求用戶通過控制台日志從顯示的列表中輸入一個選項

[英]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 中通過控制台日志顯示選項列表並要求用戶通過控制台日志從顯示的列表中輸入一個選項

預期行為 user@desktop:~$ node test.js

  1. 選項1
  2. 選項 2
  3. 選項 3

Select 選項繼續:3 您已選擇選項 3

想出一個使用查詢器模塊的方法

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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM