出于某种原因,npm询问者会在不提示用户问题的情况下进行一段时间的循环。我能想到的唯一方法(干净利落)是使用如下开关来实现它:
function callMenu (selection){
switch (selection){
case 'main':
//Use inquirer to show main menu and set selection = 'selection1' or selction = 'selection2'
callMenu(selection);
break;
case 'selection1':
//Use inquirer to show selection1 second level menu then go back to main menu
callMenu('main');
break;
case 'selection2':
//Use inquirer to show selection2 second level menu then go back to main menu
callMenu('main');
break;
}
}
callMenu('main');
问题是,我不想通过调用自身内部的函数来在调用堆栈上创建泄漏。
答案 0 :(得分:1)
您肯定在正确的轨道上。我在不同的节点模块中创建了查询子菜单,因此可以从任何提示菜单中调用它们。我还有主菜单,因为它是自己的模块,因此可以从任何子菜单中的任何位置调用它。
这就是我的mainMenu.js文件的样子:
const menu = () => {
inquirer
.prompt([{
type: 'input',
name: 'menu',
message: "Type 'Menu' or hit enter to see the menu. Type in a player's name to search for a player."
}]).then(answer => {
answer.menu === '' ? subMenu.subMenu() :
answer.menu.toLowerCase().trim() === 'menu' ? subMenu.subMenu() :
tools.quickNameLookup(answer.menu);
});
}
module.exports.menu = menu;
这是我的subMenus.js文件的样子:
const subMenu = () => {
inquirer
.prompt([{
type: 'list',
name: 'subMenu',
message: 'Welcome to the MLB stats app. What would you like to do?',
choices: ['Roster Search Menu', 'Player Search Menu', 'Player Statistics Menu', 'Leaderboards', 'Reports Menu', 'Back']
}]).then(submenu => {
let currentMenu = Object.keys(submenu)[0];
switch (submenu.subMenu) {
case 'Roster Search Menu':
rosterMenu.rosterSearchPrompt(currentMenu);
break;
case 'Player Search Menu':
playerSearchMenu.playerSearchPrompt(currentMenu);
break;
case 'Player Statistics Menu':
statisticsMenu.statsSearchPrompt(currentMenu);
break;
case 'Reports Menu':
reportsMenu.reportsPrompt(currentMenu);
break;
case 'Leaderboards':
leaderboardsMenu.leaderboardsPrompt();
case 'Back':
menu.menu();
break;
}
});
};
module.exports.subMenu = subMenu;
适用于输入提示,但是在可以避免用户错误时,我更喜欢使用列表。