vue-cli 配置
- 运行
- 全局CLI配置 `vue.config.js`
- `main.js` 入口文件配置
- 全局css `global.css`
- App.vue
- 路由
- 使用ESLint自定义校验规则
- axios 工具类
运行
- 项目的根目录(下面包含
package.json
)
- 安装依赖
cnpm install
- 运行
npm run serve
全局CLI配置 vue.config.js
- 项目的 (跟
package.json
同级的) 根目录中存在这个文件会被 @vue/cli-service 自动加载。
- 配置端口号,代理服务器
module.exports = {
outputDir: 'dist',
assetsDir: 'assets',
productionSourceMap: false,
filenameHashing: false,
lintOnSave: true,
devServer: {
open: true,
host: 'localhost',
port: 8086,
https: false,
hotOnly: false,
proxy: {
'/api': {
ws: false,
target: 'http://localhost:8081',
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
}
}
}
main.js
入口文件配置
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import locale from 'element-ui/lib/locale/lang/zh-CN'
import axios from 'axios'
import './assets/css/global.css'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
Vue.config.productionTip = (process.env.NODE_ENV !== 'production')
Vue.use(ElementUI, { locale })
Vue.prototype.$message = ElementUI.Message
Vue.prototype.$confirm = ElementUI.MessageBox.confirm
axios.defaults.baseURL = 'http://127.0.0.1:8888'
axios.interceptors.request.use(config => {
NProgress.start()
config.headers.Authorization = window.sessionStorage.getItem('token')
return config
})
axios.interceptors.response.use(config => {
NProgress.done()
return config
})
Vue.prototype.axios = axios
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
全局css global.css
html,
body,
#app {
height: 100%;
margin: 0;
padding: 0;
min-width: 1366px;
}
.el-breadcrumb {
margin-bottom: 15px;
font-size: 12px;
}
.el-card {
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
}
.el-table {
margin-top: 15px;
font-size: 12px;
}
.el-pagination {
margin-top: 15px;
}
.el-steps {
margin: 15px 0;
}
.el-step__title {
font-size: 13px;
}
.ql-editor {
min-height: 300px;
}
App.vue
<template>
<div id="app">
<router-view/>
div>
template>
<script>
export default {
name: 'app'
}
script>
<style lang="less">
style>
路由
- 创建的
router.js
或者用脚手架开始导入的vue-router依赖是创建的router/index.js
import Vue from 'vue'
import Router from 'vue-router'
const Login = () => import('./components/Login.vue')
Vue.use(Router)
const router = new Router({
routes: [
{ path: '/', redirect: '/login' },
{ path: '/login', component: Login },
{
path: '/home',
component: Home,
redirect: '/welcome',
children: [
{ path: '/welcome', component: Welcome },
{ path: '/users', component: Users },
]
}
]
})
router.beforeEach((to, from, next) => {
if (to.path === '/login') return next()
const tokenStr = window.sessionStorage.getItem('token')
if (!tokenStr) return next('/login')
next()
})
export default router
使用ESLint自定义校验规则
module.exports = {
root: true,
env: {
node: true
},
extends: [
'plugin:vue/essential',
'@vue/standard'
],
parserOptions: {
parser: 'babel-eslint'
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'eslint space-before-function-paren': ["error", "never"]
}
axios 工具类
import axios from 'axios'
axios.defaults.baseURL = 'http://127.0.0.1:8080'
import { Loading,Message } from 'element-ui'
export default {
get(url,callback,params = {}) {
const loading = Loading.service({
lock: true,
text: '数据加载中',
spinner: 'el-icon-loading',
background: 'rgba(255, 255, 255, 0.7)'
})
axios.get(url,{
params: params
}).then(response => {
if(response.data.code === 200) {
callback(response.data)
} else {
Message.error(response.data.message)
}
}).catch(err => {
Message.error(err)
}).finally(() => {
loading.close()
})
},
post(url,callback,params = {}) {
const formData = new FormData()
for(let key in params) {
formData.append(key,params[key])
}
const loading = Loading.service({
lock: true,
text: '数据提交中',
spinner: 'el-icon-loading',
background: 'rgba(255, 255, 255, 0.7)'
})
setTimeout(() => {
loading.close()
},5000)
axios.post(url,formData)
.then(response => {
if(response.data.code === 200) {
Message.success(response.data.message)
} else {
Message.error(response.data.message)
}
callback(response.data)
}).catch(err => {
Message.error(err)
}).finally(() => {
loading.close()
})
}
}
this.axios.get('/user/list',response => {
this.result.tableData = response.obj.records
this.result.pages = response.obj.pages
},this.query)