Vue项目搭建与部署
Vue 项目搭建与部署
一, 介绍与需求
1.1, 介绍
Vue 是一套用于构建用户界面的渐进式框架。与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用。Vue 两大核心思想:组件化和数据驱动。组件化就是将一个整体合理拆分为一个一个小块(组件),组件可重复使用;数据驱动是前端的未来发展方向,释放了对 DOM 的操作,让 DOM 随着数据的变化自然而然的变化(尤神原话),不必过多的关注 DOM,只需要将数据组织好即可。
vue 的 UI 组件库
1, 移动端有
(1),cube-ui 是一个基于 Vue.js 实现的精致移动端组件库。 它响应迅速、动画流畅,追求极致的交互体验。 总体分为基础、弹层、滚动三大组件模块,可以说基本涵盖了我们移动端所有的组件需求。
(2),Mint UI 包含丰富的 CSS 和 JS 组件,能够满足日常的移动端开发需要。通过它,可以快速构建出风格统一的页面,提升开发效率。
2,PC 端有
(1),Element 一套为开发者、设计师和产品经理准备的基于 Vue 2.0 的桌面端组件库
(2),iview 一套基于 Vue.js 的高质量 UI 组件库
同时也可用户微信小程序开发 iview Weapp 微信小程序 UI 组件库。小程序开发, 请看微信小程序开发的基本流程
下面主要讲 cube-ui 的配置使用
二, 项目搭建配置
2.1, 搭建环境
第一步: 安装nodeJs
第二步: 运行 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, 项目架构
| |-asserts // 用于存放静态资源,打包时会经过 webpack 处理
| |-components // 组件 存放 Vue 组件,一般是该项目公用的无状态组件
| |-entries // 入口
| |-pages // 页面视图
| |-routes // 路由 配置路由的地方
| |-services // 服务 存放服务文件,一般是网络请求等
| |-utils // 辅助工具 工具类库
|-.eslintrc.js // 代码检测配置
|-.postcssrc.js /// 添加浏览器私缀
|-index.html // 静态文件,打包时会被直接复制到输出目录中
3.2, 打包
在项目目录下运行如下命令:
1 >npm run build
如果看到如下界面,则表示成功:
3.3, 部署
部署请查看
nginx 代理部署 Vue 与 React 项目
登录成功后的效果: