使用vue-cli
来构建Vuejs
应用的项目中默认安装了eslint-loader
模块,eslint-loader
模块是目前相对比较流行的代码检测工具,能够检测书写的代码是否符合统一规范,能够在一些比较大型的项目开发中统一开发人员的代码风格,这也就是为何代码缩进有可能致使页面报错的缘由。vue
不过若稍不当心的缩进错误就致使页面报错[以下图],那开发调试效率可想而知了,有没有办法能够直接在编码的时候就发现这些小错误。答案是确定的,以sublimeText
编辑器为例,结合sublimeLinter
插件让不规范的代码先知选觉。python
ESLint
是一种用于识别ECMAScript/JavaScript
代码格式错误的工具,目的是使代码更加规范和一致并避免错误。linux
SublimeLinter
是一个代码检查框架插件,功能很是强大,支持各类语言的检查。可是它自己并无代码检查的功能,须要借助 ESLint
这样的特定语言检查支持。咱们只须要使用对应的 SublimeLinter-contrib-eslint
插件便可git
全局安装ESLintgithub
npm install eslint -g
全局安装babel-eslint。ESLint不支持Babel支持的一些语法节点。vue-cli
npm install babel-eslint –g
配置sublimeexpress
经过package control
安装SublimeLinter
,在 Sublime text
中 Ctrl + Shift + p
> Package Control:Install Package
里面搜索关键词 linter
,选择”SublimeLinter”
,注意别选成了 SummitLinter
,另外安装完成之后退出重启r'j。若搜不到直接下载包到Sublime Text 3
安装目录下:npm
cd '/path/to/Sublime Text 3/Packages' git clone https://github.com/SublimeLinter/SublimeLinter3.git SublimeLinter
同理,安装SublimeLinter-contrib-eslint
。windows
安装之后修改SublimeLinter配置文件。Preferences->PackageSettings->SublimeLinter->Settings-User
:数组
"user": { "linters": { "eslint": { "@disable":false, "args": [], "excludes": [] } }, …… "show_errors_on_save": true, //为true时,保存文件时,会弹出该文件全部不规范的列表及具体规则名 …… "mark_style": "outline", "no_column_highlights_line": false, "passive_warnings": false, "paths": { "linux": [], "osx": [], "windows": ["C:/Users/<youUserName>/AppData/Roaming/npm/eslint.cmd"] //全局安装ESLint生成的eslint.cmd的目录 }, "python_paths": { "linux": [], "osx": [], "windows": [] } }
4.配置ESLint验证规则(能够到ESLint官网本身配置rule)
到项目根目录下面使用 eslint 命令交互式的生成配置文件
生成的配置文件以下
在编辑器中已经能够看到代码格式不规范的地方了!
"quotes": [0, "single"], //建议使用单引号 "no-inner-declarations": [0, "both"], //不建议在{}代码块内部声明变量或函数 "no-extra-boolean-cast":1, //多余的感叹号转布尔型 "no-extra-semi": 1, //多余的分号 "no-extra-parens": 1, //多余的括号 "no-empty": 1, //空代码块 "no-use-before-define": [1, "nofunc"], //使用前未定义 "complexity": [1, 10], //圈复杂度大于10 警告 //常见错误 "comma-dangle": [2, "never"], //定义数组或对象最后多余的逗号 "no-debugger": 2, //debugger 调试代码未删除 "no-constant-condition":2, //常量做为条件 "no-dupe-args": 2, //参数重复 "no-dupe-keys": 2, //对象属性重复 "no-duplicate-case": 2, //case重复 "no-empty-character-class": 2, //正则没法匹配任何值 "no-invalid-regexp": 2, //无效的正则 "no-func-assign": 2, //函数被赋值 "valid-typeof": 2, //无效的类型判断 "no-unreachable": 2, //不可能执行到的代码 "no-unexpected-multiline": 2, //行尾缺乏分号可能致使一些意外状况 "no-sparse-arrays": 2, //数组中多出逗号 "no-shadow-restricted-names": 2, //关键词与命名冲突 "no-undef": 2, //变量未定义 "no-unused-vars": 2, //变量定义后未使用 "no-cond-assign": 2, //条件语句中禁止赋值操做 "no-native-reassign": 2, //禁止覆盖原生对象 //代码风格优化 "no-else-return": 1, //在else代码块中return,else是多余的 "no-multi-spaces": 1, //不容许多个空格 "key-spacing": [1, {"beforeColon": false,"afterColon": true}],//object直接量建议写法 : 后一个空格前面不留空格 "block-scoped-var": 2, //变量应在外部上下文中声明,不该在{}代码块中 "consistent-return": 2, //函数返回值多是不一样类型 "accessor-pairs": 2, //object getter/setter方法须要成对出现 "dot-location": [2, "property"], //换行调用对象方法 点操做符应写在行首 "no-lone-blocks": 2, //多余的{}嵌套 "no-empty-label": 2, //无用的标记 "no-extend-native": 2, //禁止扩展原生对象 "no-floating-decimal": 2, //浮点型须要写全 禁止.1 或 2.写法 "no-loop-func": 2, //禁止在循环体中定义函数 "no-new-func": 2, //禁止new Function(...) 写法 "no-self-compare": 2, //不允与本身比较做为条件 "no-sequences": 2, //禁止可能致使结果不明确的逗号操做符 "no-throw-literal": 2, //禁止抛出一个直接量 应是Error对象 "no-return-assign": [2, "always"], //不允return时有赋值操做 "no-redeclare": [2, {"builtinGlobals": true}],//不容许重复声明 "no-unused-expressions": [2, {"allowShortCircuit":true, "allowTernary": true}],//不执行的表达式 "no-useless-call": 2, //无心义的函数call或apply "no-useless-concat": 2, //无心义的string concat "no-void": 2, //禁用void "no-with": 2, //禁用with "space-infix-ops": 2, //操做符先后空格 "valid-jsdoc": [2, {"requireParamDescription": true,"requireReturnDescription": true}],//jsdoc "no-warning-comments": [2, { "terms":["todo", "fixme", "any other term"],"location": "anywhere" }],//标记未写注释 "curly": 1 //if、else、while、for代码块用{}包围
# 安装linter 和 linter-eslint # 须要配置环境变量(C:\Users\<youPCName>\AppData\Local\atom\bin)后才能使用apm命令 # 或者在setting中安装 $ apm install linter linter-eslint # 安装完重启Atom
此时已经能开到代码错误提示
此时只有.js文件能被校验,.vue中的<script>代码块还不能校验, 点击atom菜单栏的File
-> Settings
-> Packages
-> Community Packages
-> 找到 linter-eslint
插件,点击 Settings
-> 勾选 Lint HTML Files
,能够看到.vue文件的错误提示了。
/* eslint-disable no-new */ new Vue({ el: '#app', router, render: h => h(App) })