Unexpected console statement (no-console)

在vue cli项目中用consloe.log()打印,启动项目报错

export default {
  name: 'app',
  components: {

  },
  created() {
    this.test()
  },
  methods: {
    test () {
      console.log(_.gt(3, 1))
      console.log(_.gt(3, 3))
    }
  }
}

报错信息:

error: Unexpected console statement (no-console) at src\App.vue:21:7:
19 | methods: {
20 | test () {
> 21 | console.log(_.gt(3, 1))
| ^
22 | console.log(_.gt(3, 3))
23 | }
24 | }

解决方法:在项目中添加.eslintrc.js文件,与package.json同目录级

添加配置:

module.exports = {
  rules: {
    'no-console': 'off',
    "no-restricted-syntax": [
      "error",
      {
        "selector": "CallExpression[callee.object.name='console'][callee.property.name!=/^(log|warn|error|info|trace)$/]",
        "message": "Unexpected property on console object was called"
      }
    ]
  }
}

注意: 后面的no-restricted-syntax配置也是需要的,如果只修改"no-console": "off"

启动项目还是会报错:

error: Parsing error: Unexpected token < at src\App.vue:1:1:
> 1 | <template>
| ^

1 error found.

You may use special comments to disable some warnings.
Use // eslint-disable-next-line to ignore the next line.
Use /* eslint-disable */ to ignore all warnings in a file.

加上配置后就可以了,不同版本下需要的解决方法可能不一致,有错误时可以去官网看最新的解决方案https://eslint.org



猜你喜欢

转载自www.cnblogs.com/steamed-twisted-roll/p/11250953.html