与其使用一堆if,是否有一种方法可以为不同的选择显示不同的选项,而不是像我正在做的那样直接为每个选项指定选项?我认为有.when选项,但我不太确定如何在这种情况下使用它。
我试图让他们选择钱包选项,然后让他们查看他们选择的钱包。而且,当用户选择<<<退出键时,我似乎不知道如何将用户送回菜单的开头,因此,对如何做的任何输入都是好的。
inquirer
.prompt([
{ type:'list',
message: "Select an option",
name: 'walletOptions',
choices: [
'Wallet',
'Normal Transaction',
'Arbitrage'
]}
])
.then(({walletOptions}) => {
if (walletOptions === 'Wallet') {
//let them view and edit wallet
inquirer
.prompt([
{ type:'list',
message: "Select an account",
name: 'accountOptions',
choices: [
'1',
'2',
'3',
'<<< Quit'
]}
])
.then(({accountOptions}) => {
if (accountOptions === '1') {
//display account info for one
var walletOne = wallet[1]
console.log(walletOne);
} else if (accountOptions === '2') {
//display account info for two
var walletTwo = wallet[2]
console.log(walletTwo);
} else if (accountOptions === '3') {
//display account info for three
var walletThree = wallet[3]
console.log(walletThree);
} else {
return ;
}
})
} else if (walletOptions === 'Normal Transaction') {
//continue with mainswap.js
var normalTx = require("./mainswap");
mainswap.deploy()
} else {
//continue with ...
}
});发布于 2022-03-21 06:16:08
在type属性中使用"rawlist“而不是"list”,就像在官方例子中那样
同时,在“选择”列表之后删除不需要的"}“。
https://stackoverflow.com/questions/69868001
复制相似问题