通过 vue-cli 创建 uni-app 项目,优势可通过Jenkins部署一键发布
[官方文档][https://uniapp.dcloud.net.cn/quickstart-cli.html]
基于 vue-cli4.0 + Webpack 4 + thorui-uni + less + 请求 封装,构建手机端模板脚手架
[在线文档]](https://wz930206.github.io/uniapp-ma-template/#/)
本示例 Node.js 12.14.1
建议使用vscode 配合 HBuilder
git clone https://github.com/wz930206/uniapp-ma-template.git
cd uniapp-ma-template
npm install
Web端
npm run serve
小程序端
使用HBuilder
- √ Vue-cli4
- √ 配置多环境变量
- √ thorui 组件按需加载
- √ Less 全局样式
- √ Vuex 状态管理
- √ Webpack 4 vue.config.js 基础配置
- √ 配置 alias 别名
- √ 配置 proxy 跨域
- √ 去掉 console.log
- √ Eslint+Stylelint+Pettier 统一开发规范
以 VUE_APP_
开头的变量,在代码中可以通过 process.env.VUE_APP_
访问。
比如,VUE_APP_ENV = 'development'
通过process.env.VUE_APP_ENV
访问。
除了 VUE_APP_*
变量之外,在你的应用代码中始终可用的还有两个特殊的变量NODE_ENV
和BASE_URL
在项目根目录中新建.env.*
- .env.development 本地开发环境配置
NODE_ENV='development'
# must start with VUE_APP_
VUE_APP_ENV = 'development'
- .env.staging 测试环境配置
NODE_ENV='production'
# must start with VUE_APP_
VUE_APP_ENV = 'staging'
- .env.production 正式环境配置
NODE_ENV='production'
# must start with VUE_APP_
VUE_APP_ENV = 'production'
这里我们并没有定义很多变量,只定义了基础的 VUE_APP_ENV development
staging
production
变量我们统一在 src/config/env.*.js
里进行管理。
项目在 src/components/thorui
下统一管理组件,可查看文档
每个页面自己对应的样式都写在自己的 .vue 文件之中 scoped
它顾名思义给 css 加了一个域的概念。
<style lang="less">
/* global styles */
</style>
<style lang="less" scoped>
/* local styles */
</style>
uniapp-ma-template 所有全局样式都在 @/src/styles
目录下设置
├── styles
│ │ ├── index.less # 全局通用样式
│ │ ├── mixin.less # 全局mixin
│ │ └── _variables.less # 全局变量
目录结构
├── store
│ ├── modules
│ │ └── user.js
│ ├── index.js
│ ├── getters.js
main.js
引入
import Vue from 'vue'
import App from './App.vue'
import store from './store'
new Vue({
el: '#app',
router,
store,
render: h => h(App)
})
const path = require('path')
const resolve = dir => path.join(__dirname, dir)
module.exports = {
chainWebpack: config => {
// 添加别名
config.resolve.alias
.set('@', resolve('src'))
}
}
如果你的项目需要跨域设置,你需要打来 vue.config.js
proxy
注释 并且配置相应参数
module.exports = {
devServer: {
// ....
proxy: {
'/rest': {
target: proxyTarget, //后端接口测试环境地址 配nginx 时使用
changeOrigin: true,//是否允许跨越
pathRewrite: {
'^/rest': '/rest', //重写 不配nginx 时使用
}
}
}
}
}
使用 例如: src/api/user.js
保留了测试环境和本地环境的 console.log
在 vue.config.js 中配置
// 获取 VUE_APP_ENV 非 NODE_ENV,测试环境依然 console
const IS_PROD = ['production'].includes(process.env.VUE_APP_ENV)
module.exports = {
configureWebpack: config => {
config.optimization.minimizer[0].options.terserOptions.compress.drop_console = IS_PROD,
}
}
VScode (版本 1.47.3)安装 eslint
prettier
vetur
插件 .vue
文件使用 vetur 进行格式化,其他使用prettier
详细配置步骤可查看此博客 csdn
微信 |
---|
添加的时候备注上 urhome |