我正在使用Axios提交一个Post请求,如果请求成功,我想给用户一些确认。 但是,我对response
变量没有用处,所以我得到了一个eslint
错误。 如何解决这个问题?
axios.post('/api/option.json', {
choices: myChoices
})
.then(response => {
alert('Form was submitted successfully')
})
错误:
Module Error (from ./node_modules/eslint-loader/index.js):
error: 'response' is defined but never used (no-unused-vars) at src/components/Options.vue:78:15
编辑(2020 年 4 月(:哎呀,看起来这个问题现在有 1k 次观看但 0 个赞成票。我猜我写了一个诱人的标题,但这个问题对人们没有帮助。请评论我是否应该重命名或链接到更好的问题?
">
变量"已定义但从未使用过,此错误仅表示您声明的变量未在程序中使用。
溶液-
在程序中,使用响应作为返回值。
axios.post("/api/option.json", {
choices: myChoices;
})
.then(response => {
alert("Form was submitted successfully");
return response
});
或
axios.post("/api/option.json", {
choices: myChoices;
})
.then(() => {
alert("Form was submitted successfully");
});
或
如果您不喜欢 eslint 的此功能,可以通过将此对象添加到 package.json 文件来关闭。
"eslintConfig": {
"rules": {
"no-console": "off",
"no-unused-vars": "off"
}
},
这是我找到的最佳解决方案:
axios.post('/api/option.json', {
choices: myChoices
})
.then(() => {
alert('Form was submitted successfully')
})