搭建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/