如何解决axios.then回调上的“'响应'已定义但从未使用过”

kru*_*ubo 6 eslint axios

我正在使用 Axios 提交 Post 请求,如果请求成功,我想给用户一些确认。但是,我没有使用该response变量,因此出现eslint错误。如何解决这个问题?

axios.post('/api/option.json', {
    choices: myChoices
  })
  .then(response => {
    alert('Form was submitted successfully')
  })
Run Code Online (Sandbox Code Playgroud)

错误:

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
Run Code Online (Sandbox Code Playgroud)

编辑(2020 年 4 月):哎呀,看起来这个问题现在有 1k 次观看,但 0 人赞成。我猜我写了一个诱人的标题,但这个问题对人们没有帮助。请评论我是否应该重命名或链接到更好的问题?

Roh*_*had 10

“变量”已定义但从未使用过,此错误仅表示您声明的变量未在程序中使用。

解决方案 -

在您的程序中使用响应作为返回值。

axios.post("/api/option.json", {
  choices: myChoices;
})
.then(response => {
  alert("Form was submitted successfully");
  return response
});
Run Code Online (Sandbox Code Playgroud)

或者

axios.post("/api/option.json", {
  choices: myChoices;
})
.then(() => {
  alert("Form was submitted successfully");
});
Run Code Online (Sandbox Code Playgroud)

或者

如果你不喜欢 eslint 的这个特性,你可以通过在 package.json 文件中添加这个对象来关闭。

"eslintConfig": {
    "rules": {
      "no-console": "off",
      "no-unused-vars": "off"
    }
},
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

4427 次

最近记录:

4 年,10 月 前