Vue項目搭建與部署

Vue項目搭建與部署

一,介紹與需求

 1.1,介紹javascript

Vue  是一套用於構建用戶界面的漸進式框架。與其它大型框架不一樣的是,Vue 被設計爲能夠自底向上逐層應用。Vue兩大核心思想:組件化和數據驅動。組件化就是將一個總體合理拆分爲一個一個小塊(組件),組件可重複使用;數據驅動是前端的將來發展方向,釋放了對DOM的操做,讓DOM隨着數據的變化天然而然的變化(尤神原話),沒必要過多的關注DOM,只須要將數據組織好便可。css

vue的UI組件庫html

1,移動端有

(1),cube-ui 是一個基於 Vue.js 實現的精緻移動端組件庫。 它響應迅速、動畫流暢,追求極致的交互體驗。 整體分爲基礎、彈層、滾動三大組件模塊,能夠說基本涵蓋了咱們移動端全部的組件需求。前端

(2),Mint UI 包含豐富的 CSS 和 JS 組件,可以知足平常的移動端開發須要。經過它,能夠快速構建出風格統一的頁面,提高開發效率。vue

2,PC端有

(1),Element 一套爲開發者、設計師和產品經理準備的基於 Vue 2.0 的桌面端組件庫java

(2),iview 一套基於 Vue.js 的高質量UI 組件庫node

同時也可用戶微信小程序開發 iview Weapp 微信小程序 UI 組件庫。小程序開發,請看微信小程序開發的基本流程webpack

下面主要講cube-ui的配置使用nginx

二,項目搭建配置

 2.1,搭建環境

第一步:安裝nodeJsgit

第二步:運行cmd,打開命令行

第三步:安裝cnpm

1 npm install -g cnpm --registry=https://registry.npm.taobao.org
2 npm config set registry https://registry.npm.taobao.org

第四步:安裝vue-cli

1 cnpm install –g vue-cli

 2.2,搭建項目

vue項目的基本搭建命令以下:

  步驟:  輸入: vue init webpack 項目名稱

               如:vue init webpack firstvue

      或建立項目名稱文件夾,到目錄下運行命令:vue init webpack便可

建立web移動端項目使用vue的cube-ui移動端組件庫

第一步:初始化項目

1 # 在當前目錄下初始化一個 cube-ui 項目
2 $ vue init cube-ui/cube-template
3 # 在當前目錄下建立一個叫vue-web-app的文件夾,在裏面初始化項目
4 $ vue init cube-ui/cube-template vue-web-app

第二步:初始化時簡單配置

 1 $ vue init cube-ui/cube-template vue-web-app
 2 
 3 # 爲你的項目起個名字
 4 ? Project name vue-web-app
 5 # 起你的項目寫一段描述
 6 ? Project description A guide for vue-web-app
 7 # 做者
 8 ? Author jackson影琪 <********.com>
 9 # 選擇vue種類,第一種是運行時編譯,第二種是隻運行,建議選後者將編譯交給webpack而且體積要小大約30%
10 ? Vue build (Use arrow keys)
11 ❯ Runtime + Compiler: recommended for most users
12   Runtime-only: about 6KB lighter min+gzip, but templates (or any Vue-specific HTML) are ONL
13 Y allowed in .vue files - render functions are required elsewhere
14 # 是否後編譯
15 ? Use post-compile? Yes
16 # 按需引入組件仍是所有引入
17 ? Import type Partly
18 # 是否自定義主題,使用後編譯的狀況下可用
19 ? Custom theme? Yes
20 # rem 佈局,使用後編譯的狀況下可用
21 ? Use rem layout? No
22 # 是否安裝vue-router
23 ? Install vue-router? Yes
24 # 是否用ESLint來規範你的代碼
25 ? Use ESLint to lint your code? Yes
26 # 選擇一個ESLint預設標準
27 ? Pick an ESLint preset Standard No
28 # 是否創建單元測試
29 ? Set up unit tests Yes
30 # 是否創建端對端測試
31 ? Setup e2e tests with Nightwatch? No

第三步:安裝包並運行

1 # 安裝依賴
2 $ cnpm install
3 # 在本地的8080端口起一個有熱刷新功能的服務
4 $ npm start/npm run dev

成功後,你會看到一個有 Vue 標誌的頁面。

若是使用less,需安裝less less-loader

1 cnpm install --save-dev less less-loader

 2.3,項目配置

1,代碼檢測規範

1.1,安裝配置文件中依賴包:

    • eslint
    • babel-eslint
    • eslint-plugin-html
    • eslint-config-standard
    • eslint-plugin-standard
    • eslint-plugin-promise

經過 npm install (package) --save-dev 來配置到開發環境中。

1.2,配置.eslintrc文件

 1 module.exports = {
 2   // 默認狀況下,ESLint會在全部父級組件中尋找配置文件,一直到根目錄。ESLint一旦發現配置文件中有 "root": true,它就會中止在父級目錄中尋找。
 3   root: true,
 4   // 對Babel解析器的包裝使其與 ESLint 兼容。
 5   parser: 'babel-eslint',
 6   parserOptions: {
 7     // 代碼是 ECMAScript 模塊
 8     sourceType: 'module'
 9   },
10   env: {
11     // 預約義的全局變量,這裏是瀏覽器環境
12     browser: true,
13   },
14   // 擴展一個流行的風格指南,即 eslint-config-standard 
15   // https://github.com/feross/standard/blob/master/RULES.md#javascript-standard-style
16   extends: 'vue',
17   // required to lint *.vue files
18   plugins: [
19     // 此插件用來識別.html 和 .vue文件中的js代碼
20     'html',
21     // standard風格的依賴包
22     "standard",
23     // standard風格的依賴包
24     "promise"
25   ],
26   // add your custom rules here
27   'rules': {
28     // allow paren-less arrow functions
29     'arrow-parens': 0,
30     // allow async-await
31     'generator-star-spacing': 0,
32     // allow debugger during development
33     'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
34     "semi": [0],  // 語句能夠不須要分號結尾
35     "no-unused-vars": [0],
36     "eqeqeq": [0],
37     "array-callback-return": [0],
38     "quotes": [0],//引號風格
39     "spaced-comment": [0],
40     'comma-spacing': [0],
41     'space-before-function-paren': [0],
42     'eol-last': [0],
43     'space-infix-ops': [0],
44     "indent": 0, //強制一致的縮進風格
45     // "key-spacing": [1, {  // 對象字面量中冒號的先後空格
46     //   "beforeColon": false,
47     //   "afterColon": true
48     // }],
49     "key-spacing": [0],
50     "no-trailing-spaces": [0], //一行最後不容許有空格
51     'space-before-blocks': [0],//[2, "always"], //塊前的空格
52     'keyword-spacing': [0], //關鍵字先後的空格
53     'object-curly-spacing': [0],
54     'arrow-spacing': [0], //關鍵字先後的空格
55     'comma-dangle': [0],//[2, "never"], // 對象字面量項尾不能有逗號
56     'prefer-const': [0],//
57     'padded-blocks': [0],//[2, "never"], //塊內行首行尾是否空行
58     'no-multi-spaces': [0],// 不能用多餘的空格
59     'no-unneeded-ternary': [0],
60     "no-multiple-empty-lines": [0],//[2, {"max": 2}], //空行最多不能超過兩行
61     'block-spacing': [0],
62     'brace-style': 2,//大括號風格
63     "no-else-return": 1, // 若是if語句裏面有return,後面不能跟else語句
64   }
65 }

1.3,經常使用的規則

  1 'rules': {
  2       "comma-dangle": ["error", "never"], //是否容許對象中出現結尾逗號
  3       "no-cond-assign": 2, //條件語句的條件中不容許出現賦值運算符
  4       "no-console": 2, //不容許出現console語句
  5       "no-constant-condition": 2, //條件語句的條件中不容許出現恆定不變的量
  6       "no-control-regex": 2, //正則表達式中不容許出現控制字符
  7       "no-debugger": 2, //不容許出現debugger語句
  8       "no-dupe-args": 2, //函數定義的時候不容許出現重複的參數
  9       "no-dupe-keys": 2, //對象中不容許出現重複的鍵
 10       "no-duplicate-case": 2, //switch語句中不容許出現重複的case標籤
 11       "no-empty": 2, //不容許出現空的代碼塊
 12       "no-empty-character-class": 2, //正則表達式中不容許出現空的字符組
 13       "no-ex-assign": 2, //在try catch語句中不容許從新分配異常變量
 14       "no-extra-boolean-cast": 2, //不容許出現沒必要要的布爾值轉換
 15       "no-extra-parens": 0, //不容許出現沒必要要的圓括號
 16       "no-extra-semi": 2, //不容許出現沒必要要的分號
 17       "no-func-assign": 2, //不容許從新分配函數聲明
 18       "no-inner-declarations": ["error", "functions"], //不容許在嵌套代碼塊裏聲明函數
 19       "no-invalid-regexp": 2, //不容許在RegExp構造函數裏出現無效的正則表達式
 20       "no-irregular-whitespace": 2, //不容許出現不規則的空格
 21       "no-negated-in-lhs": 2, //不容許在in表達式語句中對最左邊的運算數使用取反操做
 22       "no-obj-calls": 2, //不容許把全局對象屬性當作函數來調用
 23       "no-regex-spaces": 2, //正則表達式中不容許出現多個連續空格
 24       "quote-props": 2, //對象中的屬性名是否須要用引號引發來
 25       "no-sparse-arrays": 2, //數組中不容許出現空位置
 26       "no-unreachable": 2, //在return,throw,continue,break語句後不容許出現不可能到達的語句
 27       "use-isnan": 2, //要求檢查NaN的時候使用isNaN()
 28       "valid-jsdoc": ["error", {
 29           "requireReturn": false,
 30           "requireParamDescription": false,
 31           "requireReturnDescription": true
 32       }], //強制JSDoc註釋
 33       "valid-typeof": ["error", {
 34           "requireStringLiterals": true
 35       }], //在使用typeof表達式比較的時候強制使用有效的字符串
 36       "block-scoped-var": 2, //將變量聲明放在合適的代碼塊裏
 37       "complexity": 0, //限制條件語句的複雜度
 38       "consistent-return": 2, //不管有沒有返回值都強制要求return語句返回一個值
 39       "curly": ["error", "all"], //強制使用花括號的風格
 40       "default-case": 0, //在switch語句中須要有default語句
 41       "dot-notation": ["error", {"allowKeywords": false, "allowPattern": ""}], //獲取對象屬性的時候使用點號
 42       "eqeqeq": ["error", "smart"], //比較的時候使用嚴格等於
 43       "no-alert": 1, //不容許使用alert,confirm,prompt語句
 44       "no-caller": 2, //不容許使用arguments.callee和arguments.caller屬性
 45       "guard-for-in": 0, //監視for in循環,防止出現不可預料的狀況
 46       "no-div-regex": 2, //不能使用看起來像除法的正則表達式
 47       "no-else-return": 0, //若是if語句有return,else裏的return不用放在else裏
 48       "no-labels": ["error", {
 49           "allowLoop": false,
 50           "allowSwitch": false
 51       }], //不容許標籤語句
 52       "no-eq-null": 2, //不容許對null用==或者!=
 53       "no-eval": 2, //不容許使用eval()
 54       "no-extend-native": 2, //不容許擴展原生對象
 55       "no-extra-bind": 2, //不容許沒必要要的函數綁定
 56       "no-fallthrough": 2, //不容許switch按順序所有執行全部case
 57       "no-floating-decimal": 2, //不容許浮點數缺失數字
 58       "no-implied-eval": 2, //不容許使用隱式eval()
 59       "no-iterator": 2, //不容許使用__iterator__屬性
 60       "no-lone-blocks": 2, //不容許沒必要要的嵌套代碼塊
 61       "no-loop-func": 2, //不容許在循環語句中進行函數聲明
 62       "no-multi-spaces": 2, //不容許出現多餘的空格
 63       "no-multi-str": 2, //不容許用\來讓字符串換行
 64       "no-global-assign": 2, //不容許從新分配原生對象
 65       "no-new": 2, //不容許new一個實例後不賦值或者不比較
 66       "no-new-func": 2, //不容許使用new Function
 67       "no-new-wrappers": 2, //不容許使用new String,Number和Boolean對象
 68       "no-octal": 2, //不容許使用八進制字面值
 69       "no-octal-escape": 2, //不容許使用八進制轉義序列
 70       "no-param-reassign": 0, //不容許從新分配函數參數"no-proto": 2, //不容許使用__proto__屬性
 71       "no-redeclare": 2, //不容許變量重複聲明
 72       "no-return-assign": 2, //不容許在return語句中使用分配語句
 73       "no-script-url": 2, //不容許使用javascript:void(0)
 74       "no-self-compare": 2, //不容許本身和本身比較
 75       "no-sequences": 2, //不容許使用逗號表達式
 76       "no-throw-literal": 2, //不容許拋出字面量錯誤 throw "error"
 77       "no-unused-expressions": 2, //不容許無用的表達式
 78       "no-void": 2, //不容許void操做符
 79       "no-warning-comments": [1, {"terms": ["todo", "fixme", "any other term"]}], //不容許警告備註
 80       "no-with": 2, //不容許使用with語句
 81       "radix": 1, //使用parseInt時強制使用基數來指定是十進制仍是其餘進制
 82       "vars-on-top": 0, //var必須放在做用域頂部
 83       "wrap-iife": [2, "any"], //當即執行表達式的括號風格
 84       "yoda": [2, "never", {"exceptRange": true}], //不容許在if條件中使用yoda條件
 85       "strict": [2, "function"], //使用嚴格模式
 86       "no-catch-shadow": 2, //不容許try catch語句接受的err變量與外部變量重名"no-delete-var": 2, //不容許使用delete操做符
 87       "no-label-var": 2, //不容許標籤和變量同名
 88       "no-shadow": 2, //外部做用域中的變量不能與它所包含的做用域中的變量或參數同名
 89       "no-shadow-restricted-names": 2, //js關鍵字和保留字不能做爲函數名或者變量名
 90       "no-undef": 2, //不容許未聲明的變量
 91       "no-undef-init": 2, //不容許初始化變量時給變量賦值undefined
 92       "no-undefined": 2, //不容許把undefined當作標識符使用
 93       "no-unused-vars": [2, {"vars": "all", "args": "after-used"}], //不容許有聲明後未使用的變量或者參數
 94       "no-use-before-define": [2, "nofunc"], //不容許在未定義以前就使用變量"indent": 2, //強制一致的縮進風格
 95       "brace-style": [2, "1tbs", { "allowSingleLine": false}], //大括號風格
 96       "camelcase": [2, {"properties": "never"}], //強制駝峯命名規則
 97       "comma-style": [2, "last"], //逗號風格
 98       "consistent-this": [0, "self"], //當獲取當前環境的this是用同樣的風格
 99       "eol-last": 2, //文件以換行符結束
100       "func-names": 0, //函數表達式必須有名字
101       "func-style": 0, //函數風格,規定只能使用函數聲明或者函數表達式
102       "key-spacing": [2, {"beforeColon": false, "afterColon": true}], //對象字面量中冒號的先後空格
103       "max-nested-callbacks": 0, //回調嵌套深度
104       "new-cap": [2, {"newIsCap": true, "capIsNew": false}], //構造函數名字首字母要大寫
105       "new-parens": 2, //new時構造函數必須有小括號
106       "newline-after-var": 0, //變量聲明後必須空一行
107       "no-array-constructor": 2, //不容許使用數組構造器
108       "no-inline-comments": 0, //不容許行內註釋
109       "no-lonely-if": 0, //不容許else語句內只有if語句
110       "no-mixed-spaces-and-tabs": [2, "smart-tabs"], //不容許混用tab和空格
111       "no-multiple-empty-lines": [2, {"max": 2}], //空行最多不能超過兩行
112       "no-nested-ternary": 2, //不容許使用嵌套的三目運算符
113       "no-new-object": 2, //禁止使用new Object()
114       "fun-call-spacing": 2, //函數調用時,函數名與()之間不能有空格
115       "no-ternary": 0, //不容許使用三目運算符
116       "no-trailing-spaces": 2, //一行最後不容許有空格
117       "no-underscore-dangle": 2, //不容許標識符如下劃線開頭
118       "no-extra-parens": 0, //不容許出現多餘的括號
119       "one-var": 0, //強制變量聲明放在一塊兒
120       "operator-assignment": 0, //賦值運算符的風格
121       "padded-blocks": [2, "never"], //塊內行首行尾是否空行
122       "quote-props": 0, //對象字面量中屬性名加引號
123       "quotes": [1, "single", "avoid-escape"], //引號風格
124       "semi": [2, "always"], //強制語句分號結尾
125       "semi-spacing": [2, {"before": false, "after": true}], //分後先後空格
126       "sort-vars": 0, //變量聲明時排序
127       "space-before-blocks": [2, "always"], //塊前的空格
128       "space-before-function-paren": [2, {"anonymous": "always", "named": "never"}], //函數定義時括號前的空格
129       "space-infix-ops": [2, {"int32Hint": true}], //操做符周圍的空格
130       "keyword-spacing": 2, //關鍵字先後的空格
131       "space-unary-ops": [2, { "words": true, "nonwords": false}], //一元運算符先後不要加空格
132       "wrap-regex": 2, //正則表達式字面量用括號括起來
133       "no-var": 0, //使用let和const代替var
134       "generator-star-spacing": [2, "both"], //生成器函數先後空格
135       "max-depth": 0, //嵌套塊深度
136       "max-len": 0, //一行最大長度,單位爲字符
137       "max-params": 0, //函數最多能有多少個參數
138       "max-statements": 0, //函數內最多有幾個聲明
139       "no-bitwise": 0, //不容許使用位運算符
140       "no-plusplus": 0 //不容許使用++ --運算符
141   }

2,開發代理配置

編輯config目錄下的index.js文件,在env裏配置以下:

1   proxyTable: {
2       "/api": {
3         "target": "http://127.0.0.1:9080",
4         "changeOrigin": true,
5       }
6     },

解決開發環境下跨域的問題

 3,打包生產環境修改

 1  build: {
 2     // Template for index.html
 3     index: path.resolve(__dirname, '../dist/index.html'),
 4 
 5     // Paths
 6     assetsRoot: path.resolve(__dirname, '../dist'),
 7     assetsSubDirectory: 'static',
 8     assetsPublicPath: './',//'/'->'./'
 9     /**
10      * Source Maps
11      */
12 
13     productionSourceMap: true,
14     // https://webpack.js.org/configuration/devtool/#production
15     devtool: '#source-map',
16 
17     // Gzip off by default as many popular static hosts such as
18     // Surge or Netlify already gzip all static assets for you.
19     // Before setting to `true`, make sure to:
20     // npm install --save-dev compression-webpack-plugin
21     productionGzip: false,
22     productionGzipExtensions: ['js', 'css'],
23 
24     // Run the build command with an extra argument to
25     // View the bundle analyzer report after build finishes:
26     // `npm run build --report`
27     // Set to `true` or `false` to always turn it on or off
28     bundleAnalyzerReport: process.env.npm_config_report
29   }

三,打包部署

3.1,項目架構

|-build             //打包配置,存放打包的文件
|-config            //項目環境配置 開發 生產 測試等 
|-node_modules             //項目包
|-public             //通常用於存放靜態文件,打包時會被直接複製到輸出目錄(./dist)
|-src               //項目源代碼
  |  |-asserts         //用於存放靜態資源,打包時會通過 webpack 處理
  |  |-caches         //緩存
  |  |-components     //組件 存放 Vue 組件,通常是該項目公用的無狀態組件
  |  |-entries        //入口
  |  |-pages          //頁面視圖
  |  |-routes         //路由 配置路由的地方
  |  |-services       //服務 存放服務文件,通常是網絡請求等
  |  |-utils          //輔助工具 工具類庫
  |  |-theme.styl          //主題樣式文件
|-static           //存放靜態資源的地方 通常是通用樣式
|-test           //測試
|-.babelrc      //vue-cli腳手架工具根目錄的babelrc配置文件
|-package.json      //包管理代碼
|-.eslintrc.js   //代碼檢測配置
|-.postcssrc.js     ///添加瀏覽器私綴
|-index.html  //靜態文件,打包時會被直接複製到輸出目錄中
|-.gitignore //Git忽略文件
|-.eslintignore //代碼檢測忽略文件

3.2,打包

在項目目錄下運行以下命令:

1 >npm run build

若是看到以下界面,則表示成功:

3.3,部署

部署請查看

nginx代理部署Vue與React項目

登陸成功後的效果:

相關文章
相關標籤/搜索