首页 > 解决方案 > Vue Cli 混淆的 ESLint 错误

问题描述

我正在将 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 =>捕捉错误吗?我看不到问题

标签: vue.jseslint

解决方案


error您必须在错误回调中对参数做一些事情。正如规则所说:

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

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

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

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

推荐阅读