Vuejs报错error: Unexpected console statement (no-console) at src\... 解决办法

当vue报这个错误的时候,还是可以运行的,但是看着难受,所以决定关掉它。这个错误其实是Vuejs - 使用ESLint检查代码质量,如果你有看es6编程规则的话,应该可以看到下面这段话:

ESLint 是一个语法规则和代码风格的检查工具,可以用来保证写出语法正确、风格统一的代码。

首先,安装 ESLint。

$ npm i -g eslint 然后,安装 Airbnb 语法规则,以及 import、a11y、react 插件。

$ npm i -g eslint-config-airbnb $ npm i -g eslint-plugin-import
eslint-plugin-jsx-a11y eslint-plugin-react
最后,在项目的根目录下新建一个.eslintrc文件,配置 ESLint。

{ “extends”: “eslint-config-airbnb” } 现在就可以检查,当前项目的代码是否符合预设的规则。

index.js文件的代码如下。

var unusued = ‘I have no purpose!’;

function greet() {
var message = ‘Hello, World!’;
alert(message); }

greet(); 使用 ESLint 检查这个文件,就会报出错误。

$ eslint index.js index.js 1:1 error Unexpected var, use let or
const instead no-var 1:5 error unusued is defined but
never used no-unused-vars 4:5 error Expected
indentation of 2 characters but found 4 indent 4:5 error
Unexpected var, use let or const instead no-var 5:5 error
Expected indentation of 2 characters but found 4 indent

✖ 5 problems (5 errors, 0 warnings)

上面代码说明,原文件有五个错误,其中两个是不应该使用var命令,而要使用let或const;一个是定义了变量,却没有使用;另外两个是行首缩进为
4 个空格,而不是规定的 2 个空格。

也就是说,是ESLint检查出你的代码风格不符合这个规则。
但是调试中又得使用console.log
所以我们可以如下图操作关闭。
Vuejs报错error: Unexpected console statement (no-console) at src\... 解决办法
改成”no-console“:”off“
这样就不会报错了。
但是养成一个很好的编码风格还是很重要的。