[英]how to call another page function name in inquirer node js
我正在使用 nodejs 中的查詢器制作 CLI。
因此,在每個選擇列表中,我都必須給出退出選擇,因此如果用戶想退出,他/她可以輕松退出。
所以我必須一次又一次地編寫 Exit 來避免這個問題,我創建了一個Exit.js文件並將 Exit 代碼移到那里,這樣我就可以一次又一次地使用代碼。
const executeQuery = require("../executeQuery"); function WantToExit() { inquirer.prompt([ { name: "moreQuery", type: "confirm", message: "Want to do anything else?", }, ]).then((answer) => { if (answer.moreQuery) return executeQuery(); }); } module.exports = WantToExit;
我的 executeQuery 代碼看起來像這樣
const wantToExit = require("../Exit"); const Science = require("../Science"); function executetQuery() { inquirer.prompt([ { type: "list", name: "cmsType", message: " Select Subject Options ", default: false, choices: ["Science", "Maths", "English", "Exit"], }, ]).then((answers) => { if (answers.cmsType === "Science") { Science(); } else if (answers.cmsType === "Exit") { wantToExit(); } }); } module.exports = executetQuery;
當我 select 從executeQuery選項退出並按 Y選項時,我從Exit.js文件中收到此錯誤
if (answer.moreQuery) return executeQuery();
^
TypeError: executeQuery is not a function
at /home/admin/SchoolProject/src/Exit/index.js:13:36
這是一個循環依賴的場景。 A 需要 B,B 需要 A,以此類推。 要使其正常工作,您必須修改 module.exports。
在 Exit.js 文件中,將 module.exports=WantToExit 更改為module.exports.WantToExit = WantToExit
並在 ExecuteQuery.js 文件中將其要求為const {WantToExit} =require('./Exit.js'
)。
類似, module.exports.ExecuteQuery=ExecuteQuery
和 require as const {ExecuteQuery} =require('./ExecuteQuery.js')
您的方法存在問題,因為它產生了模塊的循環依賴。 您在 ExecuteQuery.js 中有“必需”的 wantToExit,在 Exit.js 中有“必需”的 executetQuery()
我相信你想要實現的是不斷詢問用戶他喜歡的主題,然后根據他/她的選擇做一些事情,直到用戶選擇退出。
我建議在 ExecuteQuery.js 中使用 while 循環作為主提示,並使用 boolean 標志來檢查用戶是否要退出。
const wantToExit = require("../Exit");
const Science = require("../Science");
function executetQuery() {
let toStop = false;
// use a while loop
while(!toStop) {
inquirer
.prompt([
{
type: "list",
name: "cmsType",
message: " Select Subject Options ",
default: false,
choices: ["Science", "Maths", "English", "Exit"],
},
])
.then(async (answers) => {
if (answers.cmsType === "Science") {
// you can also set toStop = true here if you want to
// stop after first iteration
Science();
} else if (answers.cmsType === "Exit") {
// wantToExit() now returns a boolean flag
toStop = await wantToExit();
}
});
}
}
module.exports = executetQuery;
你的 Exit.js 應該像
function WantToExit() {
inquirer
.prompt([
{
name: "moreQuery",
type: "confirm",
message: "Want to do anything else?",
},
])
.then((answer) => {
return !answer.moreQuery;
});
}
module.exports = WantToExit;
我的指導是學習 RXJS 並以某種方式觀察到這一點。
另外我認為 (yield* ) 可能在嚴格模式下工作不確定,我不想這樣做,因為這更像是一個建議和研究
生成器函數* 探索 ES6 © 2015 - 2018 Axel Rauschmayer (封面由 Fran Caye)
const { Observable } = require("rxjs");
async function* wantToExit() {
(yield* await inquirer
.prompt([
{
name: "moreQuery",
type: "confirm",
message: "Want to do anything else?",
},
])
.then(answer => answer.moreQuery)
);
}
const executeQuery = new Observable(subscriber => {
inquirer.prompt([
{
type: "list",
name: "cmsType",
message: " Select Subject Options ",
default: false,
choices: ["Science", "Maths", "English", "Exit"],
},
]).then((answers) => {
if (answers.cmsType === "Science") {
subscriber.next(answers.cmsType);
} else if (answers.cmsType === "Exit") {
let doWeExit = await wantToExit().next();
if (doWeExit === ADD_SOMETHING_NO) {
executeQuery.subscribe(userResponse => userResponse);
} else {
console.log('Adios!');
return false;
}
}
});
});
module.exports = { executeQuery };
在一個新的頁面上,你可以做到。 或者您可以在 function 聲明下直接使用它。 希望這對下一步有所幫助。
const {executeQuery} = require('{INCLUDE YOUR FILEPATH}'); executeQuery.subscribe(userResponse => { if(userResponse === 'Science') science(); console.log(userResponse); });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.