webpack4.0
Pan 2018-08-15 18:39:24 +08:00
parent ba71a4b5bc
commit 1eac664f65
8 changed files with 76 additions and 52 deletions

View File

@ -1,29 +1,30 @@
# vueAdmin-template # vueAdmin-template
> 这是一个 极简的vue admin 管理后台 它只包含了 Element UI & axios & iconfont & permission control & lint这些搭建后台必要的东西。 > 这是一个 极简的 vue admin 管理后台 它只包含了 Element UI & axios & iconfont & permission control & lint这些搭建后台必要的东西。
[线上地址](http://panjiachen.github.io/vueAdmin-template) [线上地址](http://panjiachen.github.io/vueAdmin-template)
## Extra ## Extra
如果你想要根据用户角色来动态生成侧边栏和router你可以使用改分支[permission-control](https://github.com/PanJiaChen/vueAdmin-template/tree/permission-control)
 
## 相关项目
[vue-element-admin](https://github.com/PanJiaChen/vue-element-admin)
[electron-vue-admin](https://github.com/PanJiaChen/electron-vue-admin) 如果你想要根据用户角色来动态生成侧边栏和 router你可以使用改分支[permission-control](https://github.com/PanJiaChen/vueAdmin-template/tree/permission-control)
## 相关项目
[vue-element-admin](https://github.com/PanJiaChen/vue-element-admin)
[electron-vue-admin](https://github.com/PanJiaChen/electron-vue-admin)
写了一个系列的教程配套文章,如何从零构建后一个完整的后台项目: 写了一个系列的教程配套文章,如何从零构建后一个完整的后台项目:
- [手摸手,带你用 vue 撸后台 系列一(基础篇)](https://juejin.im/post/59097cd7a22b9d0065fb61d2)
- [手摸手,带你用 vue 撸后台 系列二(登录权限篇)](https://juejin.im/post/591aa14f570c35006961acac)
- [手摸手,带你用 vue 撸后台 系列三 (实战篇)](https://juejin.im/post/593121aa0ce4630057f70d35)
- [手摸手带你用vue撸后台 系列四(vueAdmin 一个极简的后台基础模板,专门针对本项目的文章,算作是一篇文档)](https://juejin.im/post/595b4d776fb9a06bbe7dba56)
- [手摸手带你封装一个vue component](https://segmentfault.com/a/1190000009090836)
- [手摸手,带你用 vue 撸后台 系列一(基础篇)](https://juejin.im/post/59097cd7a22b9d0065fb61d2)
- [手摸手,带你用 vue 撸后台 系列二(登录权限篇)](https://juejin.im/post/591aa14f570c35006961acac)
- [手摸手,带你用 vue 撸后台 系列三 (实战篇)](https://juejin.im/post/593121aa0ce4630057f70d35)
- [手摸手,带你用 vue 撸后台 系列四(vueAdmin 一个极简的后台基础模板,专门针对本项目的文章,算作是一篇文档)](https://juejin.im/post/595b4d776fb9a06bbe7dba56)
- [手摸手,带你封装一个 vue component](https://segmentfault.com/a/1190000009090836)
## Build Setup ## Build Setup
``` bash ```bash
# Clone project # Clone project
git clone https://github.com/PanJiaChen/vueAdmin-template.git git clone https://github.com/PanJiaChen/vueAdmin-template.git
@ -44,14 +45,17 @@ npm run build --report
``` ```
## Demo ## Demo
![demo](https://github.com/PanJiaChen/PanJiaChen.github.io/blob/master/images/demo.gif) ![demo](https://github.com/PanJiaChen/PanJiaChen.github.io/blob/master/images/demo.gif)
### Element-Ui 使用cdn教程 ### Element-Ui 使用 cdn 教程
首先找到 `index.html` ([根目录下](https://github.com/PanJiaChen/vueAdmin-template/blob/element-ui-cdn/index.html)) 首先找到 `index.html` ([根目录下](https://github.com/PanJiaChen/vueAdmin-template/blob/element-ui-cdn/index.html))
引入 Element的css和js ,并且引入 vue 。因为 Element-Ui 是依赖 vue 的,所以必须在它之前引入 vue 。 引入 Element 的 css 和 js ,并且引入 vue 。因为 Element-Ui 是依赖 vue 的,所以必须在它之前引入 vue 。
之后找到 [webpack.base.conf.js](https://github.com/PanJiaChen/vueAdmin-template/blob/element-ui-cdn/build/webpack.base.conf.js) 加入 `externals` 让 webpack 不打包 vue 和 element
之后找到 [webpack.base.conf.js](https://github.com/PanJiaChen/vueAdmin-template/blob/element-ui-cdn/build/webpack.base.conf.js) 加入 `externals` 让webpack 不打包 vue 和 element
``` ```
externals: { externals: {
vue: 'Vue', vue: 'Vue',
@ -59,7 +63,7 @@ externals: {
} }
``` ```
之后还有一个小细节是如果你用了全局对象方式引入vue就不需要 手动 `Vue.use(Vuex` ,它会自动挂载,具体见 [issue](https://github.com/vuejs/vuex/issues/731) 之后还有一个小细节是如果你用了全局对象方式引入 vue就不需要 手动 `Vue.use(Vuex` ,它会自动挂载,具体见 [issue](https://github.com/vuejs/vuex/issues/731)
最终你可以使用 `npm run build --report` 查看效果 最终你可以使用 `npm run build --report` 查看效果
如图: 如图:
@ -70,8 +74,7 @@ externals: {
**[对应分支](https://github.com/PanJiaChen/vueAdmin-template/tree/element-ui-cdn)** **[对应分支](https://github.com/PanJiaChen/vueAdmin-template/tree/element-ui-cdn)**
## License ## License
[MIT](https://github.com/PanJiaChen/vueAdmin-template/blob/master/LICENSE) license. [MIT](https://github.com/PanJiaChen/vueAdmin-template/blob/master/LICENSE) license.
Copyright (c) 2017-present PanJiaChen Copyright (c) 2017-present PanJiaChen

View File

@ -8,8 +8,7 @@
## Build Setup ## Build Setup
``` bash ```bash
# Clone project # Clone project
git clone https://github.com/PanJiaChen/vueAdmin-template.git git clone https://github.com/PanJiaChen/vueAdmin-template.git
@ -27,17 +26,21 @@ npm run build --report
``` ```
## Demo ## Demo
![demo](https://github.com/PanJiaChen/PanJiaChen.github.io/blob/master/images/demo.gif) ![demo](https://github.com/PanJiaChen/PanJiaChen.github.io/blob/master/images/demo.gif)
## Extra ## Extra
If you want router permission && generate menu by user roles , you can use this branch [permission-control](https://github.com/PanJiaChen/vueAdmin-template/tree/permission-control) If you want router permission && generate menu by user roles , you can use this branch [permission-control](https://github.com/PanJiaChen/vueAdmin-template/tree/permission-control)
## Related Project ## Related Project
[vue-element-admin](https://github.com/PanJiaChen/vue-element-admin)
[electron-vue-admin](https://github.com/PanJiaChen/electron-vue-admin) [vue-element-admin](https://github.com/PanJiaChen/vue-element-admin)
[electron-vue-admin](https://github.com/PanJiaChen/electron-vue-admin)
### Element-Ui using cdn tutorial ### Element-Ui using cdn tutorial
First find `index.html`([root directory](https://github.com/PanJiaChen/vueAdmin-template/blob/element-ui-cdn/index.html)) First find `index.html`([root directory](https://github.com/PanJiaChen/vueAdmin-template/blob/element-ui-cdn/index.html))
Import css and js of `Element`, and then import vue. Because `Element` is vue-dependent, vue must be import before it. Import css and js of `Element`, and then import vue. Because `Element` is vue-dependent, vue must be import before it.
@ -53,7 +56,7 @@ externals: {
``` ```
Finally there is a small detail to pay attention to that if you import vue in global, you don't need to manually `Vue.use(Vuex)`, it will be automatically mounted, see Finally there is a small detail to pay attention to that if you import vue in global, you don't need to manually `Vue.use(Vuex)`, it will be automatically mounted, see
[issue](https://github.com/vuejs/vuex/issues/731) [issue](https://github.com/vuejs/vuex/issues/731)
And you can use `npm run build --report` to see the effect And you can use `npm run build --report` to see the effect
@ -64,8 +67,8 @@ Pictured:
**[Branch](https://github.com/PanJiaChen/vueAdmin-template/tree/element-ui-cdn)** **[Branch](https://github.com/PanJiaChen/vueAdmin-template/tree/element-ui-cdn)**
## License ## License
[MIT](https://github.com/PanJiaChen/vueAdmin-template/blob/master/LICENSE) license. [MIT](https://github.com/PanJiaChen/vueAdmin-template/blob/master/LICENSE) license.
Copyright (c) 2017-present PanJiaChen Copyright (c) 2017-present PanJiaChen

View File

@ -6,7 +6,6 @@ const path = require('path')
module.exports = { module.exports = {
dev: { dev: {
// Paths // Paths
assetsSubDirectory: 'static', assetsSubDirectory: 'static',
assetsPublicPath: '/', assetsPublicPath: '/',
@ -40,7 +39,7 @@ module.exports = {
// (https://github.com/webpack/css-loader#sourcemaps) // (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected, // In our experience, they generally work as expected,
// just be aware of this issue when enabling this option. // just be aware of this issue when enabling this option.
cssSourceMap: false, cssSourceMap: false
}, },
build: { build: {
@ -80,6 +79,8 @@ module.exports = {
// `npm run build --report` // `npm run build --report`
// Set to `true` or `false` to always turn it on or off // Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report || false, bundleAnalyzerReport: process.env.npm_config_report || false,
// `npm run build:prod --generate_report`
generateAnalyzerReport: process.env.npm_config_generate_report || false generateAnalyzerReport: process.env.npm_config_generate_report || false
} }
} }

View File

@ -1,5 +1,5 @@
import Vue from 'vue' import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'// svg组件 import SvgIcon from '@/components/SvgIcon' // svg组件
// register globally // register globally
Vue.component('svg-icon', SvgIcon) Vue.component('svg-icon', SvgIcon)

View File

@ -1,6 +1,6 @@
import Vue from 'vue' import Vue from 'vue'
import 'normalize.css/normalize.css'// A modern alternative to CSS resets import 'normalize.css/normalize.css' // A modern alternative to CSS resets
import ElementUI from 'element-ui' import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css' import 'element-ui/lib/theme-chalk/index.css'

View File

@ -25,7 +25,7 @@ export function parseTime(time, cFormat) {
} }
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => { const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
let value = formatObj[key] let value = formatObj[key]
if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1] if (key === 'a') { return ['一', '二', '三', '四', '五', '六', '日'][value - 1] }
if (result.length > 0 && value < 10) { if (result.length > 0 && value < 10) {
value = '0' + value value = '0' + value
} }
@ -43,7 +43,8 @@ export function formatTime(time, option) {
if (diff < 30) { if (diff < 30) {
return '刚刚' return '刚刚'
} else if (diff < 3600) { // less 1 hour } else if (diff < 3600) {
// less 1 hour
return Math.ceil(diff / 60) + '分钟前' return Math.ceil(diff / 60) + '分钟前'
} else if (diff < 3600 * 24) { } else if (diff < 3600 * 24) {
return Math.ceil(diff / 3600) + '小时前' return Math.ceil(diff / 3600) + '小时前'
@ -53,6 +54,16 @@ export function formatTime(time, option) {
if (option) { if (option) {
return parseTime(time, option) return parseTime(time, option)
} else { } else {
return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分' return (
d.getMonth() +
1 +
'月' +
d.getDate() +
'日' +
d.getHours() +
'时' +
d.getMinutes() +
'分'
)
} }
} }

View File

@ -10,23 +10,26 @@ const service = axios.create({
}) })
// request拦截器 // request拦截器
service.interceptors.request.use(config => { service.interceptors.request.use(
if (store.getters.token) { config => {
config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改 if (store.getters.token) {
config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
}
return config
},
error => {
// Do something with request error
console.log(error) // for debug
Promise.reject(error)
} }
return config )
}, error => {
// Do something with request error
console.log(error) // for debug
Promise.reject(error)
})
// respone拦截器 // respone拦截器
service.interceptors.response.use( service.interceptors.response.use(
response => { response => {
/** /**
* code为非20000是抛错 可结合自己业务进行修改 * code为非20000是抛错 可结合自己业务进行修改
*/ */
const res = response.data const res = response.data
if (res.code !== 20000) { if (res.code !== 20000) {
Message({ Message({
@ -37,13 +40,17 @@ service.interceptors.response.use(
// 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了; // 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了;
if (res.code === 50008 || res.code === 50012 || res.code === 50014) { if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', { MessageBox.confirm(
confirmButtonText: '重新登录', '你已被登出,可以取消继续留在该页面,或者重新登录',
cancelButtonText: '取消', '确定登出',
type: 'warning' {
}).then(() => { confirmButtonText: '重新登录',
cancelButtonText: '取消',
type: 'warning'
}
).then(() => {
store.dispatch('FedLogOut').then(() => { store.dispatch('FedLogOut').then(() => {
location.reload()// 为了重新实例化vue-router对象 避免bug location.reload() // 为了重新实例化vue-router对象 避免bug
}) })
}) })
} }
@ -53,7 +60,7 @@ service.interceptors.response.use(
} }
}, },
error => { error => {
console.log('err' + error)// for debug console.log('err' + error) // for debug
Message({ Message({
message: error.message, message: error.message,
type: 'error', type: 'error',

View File

@ -30,4 +30,3 @@ export function validatAlphabets(str) {
const reg = /^[A-Za-z]+$/ const reg = /^[A-Za-z]+$/
return reg.test(str) return reg.test(str)
} }