【问题标题】:ESLint error for Vue Cli confusionVue Cli 混淆的 ESLint 错误
【发布时间】:2018-12-14 19:14:20
【问题描述】:

我正在将 Vue Cli 用于一个包含 .eslintrc.js 的项目,我正在努力坚持。它在下面的代码中发现了一个错误。

getTags (url) {
    axios.get('http://example.test/api' + url).then(response => {
        this.tags = response.data.data
    }).catch(error => {
        this.tags = []
    })
}

确切的错误是这部分代码(在.catch之后)

(error => {
    this.tags = []
    this.loadingTags = false
})

错误是[eslint] Expected error to be handled. (handle-callback-err)

但我很困惑,.catch(error => 没有发现错误吗?我看不到问题

【问题讨论】:

  • 也许是因为您没有对error 参数本身做任何事情?例如,尝试记录它(console.log(error))。

标签: vue.js eslint


【解决方案1】:

您必须在错误回调中使用error 参数做一些事情。正如rule 所说:

在 Node.js 中,一种处理异步行为的常用模式 称为回调模式。此模式需要一个 Error 对象 或 null 作为回调的第一个参数。忘记处理 这些错误可能会导致你的一些非常奇怪的行为 应用。

因此您可以记录它或在您创建的处理程序中处理它:

(error => {
    console.log(error)
    // or
    this.handleError(error)

    this.tags = []
    this.loadingTags = false
})

【讨论】:

    【解决方案2】:

    有时您只是不想对错误做任何事情。在这些情况下,您可以这样做:

    // eslint-disable-next-line
    (error => {
        this.tags = []
        this.loadingTags = false
    })
    

    在代码中包含这些 cmets 并不漂亮,但您将摆脱那些您只知道不是“真实”的警告/错误。

    【讨论】:

      猜你喜欢
      • 2020-06-05
      • 2020-10-27
      • 2019-01-23
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多