搭建vue项目开发可能选择vue-cli项目脚手架快速建立vue项目。(https://github.com/vuejs/vue-...html
npm install -g vue-cli
这种方式安装比较慢,能够用国内淘宝镜像安装,cnpm,安装cnpmvue
npm install -g cnpm --registry=https://registry.npm.taobao.org
继续安装webpack
cnpm install -g vue-cli
vue-cli脚手架自带webpack打包工具,建立一个基于webpack模板的新项目git
vue init webpack my-project
这里须要进行一些配置,默认回车便可github
This will install Vue 2.x version of the template. For Vue 1.x use: vue init webpack#1.0 my-project ? Project name my-project ? Project description A Vue.js project ? Author runoob <test@runoob.com> ? Vue build standalone ? Use ESLint to lint your code? Yes ? Pick an ESLint preset Standard ? Setup unit tests with Karma + Mocha? Yes ? Setup e2e tests with Nightwatch? Yes vue-cli · Generated "my-project". To get started: cd my-project npm install npm run dev Documentation can be found at https://vuejs-templates.github.io/webpack
## 配置esLint ##web
eslint配置方式主要有两种:vue-cli
通常须要咱们去配置项包括:npm
vue-cli脚手架安装完成后,主要有几个地方配置了eslint。json
.eslintignore相似Git的.gitignore用来配置不须要Eslint检查的文件
.eslintrc.js主要用来配置具体规则浏览器
.eslintignore文件
添加不启动静态检查的文件
build/*.js // 忽略build文件夹下全部的脚本文件 config/*.js
.eslintrc.js文件
// https://eslint.org/docs/user-guide/configuring module.exports = { root: true, parser: 'babel-eslint', // 解析器为babel-eslint,可在package.json文件中配置 parserOptions: { sourceType: 'module' }, env: { //环境配置为浏览器 browser: true, }, // https://github.com/standard/standard/blob/master/docs/RULES-en.md extends: 'standard', //文件配置继承standard规则,具体访问链接 // required to lint *.vue files plugins: [ 'html' ], // add your custom rules here 'rules': { // 添加自定义规则 // allow paren-less arrow functions 'arrow-parens': 0, // allow async-await 'generator-star-spacing': 0, // allow debugger during development 'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0 } }
说明: 在rules中每一个配置项后面的第一个值为eslint规则的错误等级
"script" : { "lint": "eslint --ext .js, .vue src" } "devDenpendencies" : { "babel-eslint": "^7.1.1", // 更多eslint文件 ... }
#### 3 在webpack配置文件中 ####
webpack.base.conf.js
module: { rules: [ { test: /\.(js|vue)$/, loader: 'eslint-loader', enforce: 'pre', include: [resolve('src'), resolve('test')], options: { formatter: require('eslint-friendly-formatter') } } ] }
有时候代码里有些特殊状况须要咱们在某一行或者某几行关闭ESLint检测,能够使用注释:
1,注释关闭全部规则
/* eslint-disable */ alert('foo'); /* eslint-enable */
2,关闭某一行的全部规则
alert('foo'); // eslint-disable-line // eslint-disable-next-line alert('foo');
3,在某一行关闭指定的规则
alert('foo'); // eslint-disable-line no-alert // eslint-disable-next-line no-alert alert('foo');
经常使用规则:
规则的细节请到ESLint官方网站查看 http://eslint.org/docs/rules/