问题描述
我正在使用 inquirer.js
为 CLI 脚本创建一个简单的提示。我试图在 switch 语句中设置一个变量值,然后在 axios 实例配置中使用这个变量。
我面临变量总是未定义的问题,我该如何正确设置它?
let url;
switch( answers.language ){
case 'Italian':
url = 'https://it.wikipedia.org/w/api.PHP';
break;
case 'English':
url = 'https://en.wikipedia.org/w/api.PHP';
break;
case 'Deutch':
url = 'https://de.wikipedia.org/w/api.PHP';
break;
}
axios({
url: url,method: 'GET',params: {
action: 'query',list: 'search',srsearch: answers.search,format: 'json'
}
}).then( (response) => {
console.log(response);
console.log(response.data);
});
解决方法
您需要在开关盒中添加一个默认选项。例如:
switch(type)
{
case 1:
//something
case 2:
//something else
default:
// default option.
}