vue create vue3-admin
更改前
更改后
2、开发项目
npm install element-plus --save
cnpm install -D unplugin-vue-components unplugin-auto-import
const AutoImport = require('unplugin-auto-import/webpack')
const Components = require('unplugin-vue-components/webpack')
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')module.exports = {
intOnSave:false,devServer:{open:true,port:9000,},configureWebpack: {plugins: [AutoImport({resolvers: [ElementPlusResolver()],}),Components({resolvers: [ElementPlusResolver()],}),]},
}
tips:在main.js中引入样式(避免弹出框等组件样式错乱)
import 'element-plus/dist/index.css'
cnpm install @element-plus/icons-vue
//main.js
import * as ELIcons from '@element-plus/icons-vue'
for (const iconName in ELIcons) {app.component(iconName, ELIcons[iconName])
}
import '@/styles/index.scss'
css: {loaderOptions: {sass: {// 8版本用prependData:prependData: `@import "@/styles/variables.scss"; // scss文件地址@import "@/styles/mixin.scss"; // scss文件地址`}}}
import SvgIcon from '@/components/SvgIcon'const svgRequired = require.context('./svg', false, /\.svg$/)
svgRequired.keys().forEach((item) => svgRequired(item))export default (app) => {app.component('svg-icon', SvgIcon)
}
import SvgIcon from '@/icons'
const app=createApp(App)
SvgIcon(app)
cnpm i --save-dev svg-sprite-loader@6.0.9
const path = require('path')
function resolve(dir) {return path.join(__dirname, dir)
}
const webpack = require('webpack')module.exports = {
chainWebpack(config) {// 设置 svg-sprite-loader// config 为 webpack 配置对象// config.module 表示创建一个具名规则,以后用来修改规则config.module// 规则.rule('svg')// 忽略.exclude.add(resolve('src/icons'))// 结束.end()// config.module 表示创建一个具名规则,以后用来修改规则config.module// 规则.rule('icons')// 正则,解析 .svg 格式文件.test(/\.svg$/)// 解析的文件.include.add(resolve('src/icons'))// 结束.end()// 新增了一个解析的loader.use('svg-sprite-loader')// 具体的loader.loader('svg-sprite-loader')// loader 的配置.options({symbolId: 'icon-[name]'})// 结束.end()config.plugin('ignore').use(new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /zh-cn$/))config.module.rule('icons').test(/\.svg$/).include.add(resolve('src/icons')).end().use('svg-sprite-loader').loader('svg-sprite-loader').options({symbolId: 'icon-[name]'}).end()},
}
cnpm i axios --save
const TOKEN_TIME = 'tokenTime'const TOKEN_TIME_VALUE = 2 * 60 * 60 * 1000// 登录时设置时间
export const setTokenTime = () => {localStorage.setItem(TOKEN_TIME, Date.now())
}// 获取
export const getTokenTime = () => {return localStorage.getItem(TOKEN_TIME)
}// 是否已经过期
export const diffTokenTime = () => {const currentTime = Date.now()const tokenTime = getTokenTime()return currentTime - tokenTime > TOKEN_TIME_VALUE
}
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { diffTokenTime } from '@/utils/auth'
import store from '@/store'
const service = axios.create({baseURL: process.env.VUE_APP_BASE_API,timeout: 5000
})service.interceptors.request.use((config) => {if (localStorage.getItem('token')) {if (diffTokenTime()) {store.dispatch('app/logout')return Promise.reject(new Error('token 失效了'))}}config.headers.Authorization = localStorage.getItem('token')return config},(error) => {return Promise.reject(new Error(error))}
)service.interceptors.response.use((response) => {const { data, meta } = response.dataif (meta.status === 200 || meta.status === 201) {return data} else {ElMessage.error(meta.msg)return Promise.reject(new Error(meta.msg))}},(error) => {console.log(error.response)error.response && ElMessage.error(error.response.data)return Promise.reject(new Error(error.response.data))}
)
export default service
devServer: {https: false,hotOnly: false,open:true,port:9000,proxy: {'/api': {target: 'https://*******/api/private/v1/',changeOrigin: true,pathRewrite: {'^/api': ''}}}},
新建 .env.development、.env.production文件
ENV = 'development'VUE_APP_BASE_API = '/api'
ENV = 'production'VUE_APP_BASE_API = '/prod-api'
import { createRouter, createWebHashHistory } from 'vue-router'const routes = [{path: '/login',name: 'Login',component: () => import('../views/login/index.vue')},{path: '/',name: '/',component: () => import('../layout'),redirect: '/users',children: [{path: 'users',name: 'users',component: () => import('@/views/users/index.vue')},{path: 'categories',name: 'categories',component: () => import('@/views/categories/index.vue')},{path: 'goods',name: 'goods',component: () => import('@/views/goods/index.vue')},{path: 'orders',name: 'orders',component: () => import('@/views/orders/index.vue')},{path: 'params',name: 'params',component: () => import('@/views/params/index.vue')},{path: 'reports',name: 'reports',component: () => import('@/views/reports/index.vue')},{path: 'rights',name: 'rights',component: () => import('@/views/rights/index.vue')},{path: 'roles',name: 'roles',component: () => import('@/views/roles/index.vue')}]}
]const router = createRouter({history: createWebHashHistory(),routes
})export default router
import router from './index'
import store from '@/store'const whiteList = ['/login']
router.beforeEach((to, from, next) => {if (store.getters.token) {if (to.path === '/login') {next('/')} else {next()}} else {if (whiteList.includes(to.path)) {next()} else {next('/login')}}
})
import '@/router/permission'
后台管理系统
import request from './request'export const login = (data) => {return request({url: '/login',method: 'POST',data})
}
import { login as loginApi } from '@/api/login'
import router from '@/router'
import { setTokenTime } from '@/utils/auth'
export default {namespaced: true,state: () => ({token: localStorage.getItem('token') || '',siderType: true,}),mutations: {setToken(state, token) {state.token = tokenlocalStorage.setItem('token', token)},changeSiderType(state) {state.siderType = !state.siderType},},actions: {login({ commit }, userInfo) {return new Promise((resolve, reject) => {loginApi(userInfo).then((res) => {console.log(res)commit('setToken', res.token)setTokenTime()router.replace('/')resolve()}).catch((err) => {reject(err)})})},// 退出logout({ commit }) {commit('setToken', '')localStorage.clear()router.replace('/login')}}
}
export default {token: (state) => state.app.token,siderType: (state) => state.app.siderType,
}
import { createStore } from 'vuex'
import app from './modules/app'
import getters from './getters'
export default createStore({modules: {app},getters
})
menu数据
{"data": [{"id": 125,"authName": "用户管理","path": "users","children": [{"id": 110,"authName": "用户列表","path": "users","children": [],"order": null}],"order": 1},{"id": 103,"authName": "权限管理","path": "rights","children": [{"id": 111,"authName": "角色列表","path": "roles","children": [],"order": null},{"id": 112,"authName": "权限列表","path": "rights","children": [],"order": null}],"order": 2},{"id": 101,"authName": "商品管理","path": "goods","children": [{"id": 104,"authName": "商品列表","path": "goods","children": [],"order": 1},{"id": 115,"authName": "分类参数","path": "params","children": [],"order": 2},{"id": 121,"authName": "商品分类","path": "categories","children": [],"order": 3}],"order": 3},{"id": 102,"authName": "订单管理","path": "orders","children": [{"id": 107,"authName": "订单列表","path": "orders","children": [],"order": null}],"order": 4},{"id": 145,"authName": "数据统计","path": "reports","children": [{"id": 146,"authName": "数据报表","path": "reports","children": [],"order": null}],"order": 5}],"meta": {"msg": "获取菜单列表成功","status": 200}
}
1、安装
cnpm i screenfull --save
2、使用
1、安装
cnpm i driver.js --save
2、使用
4、用户管理开发
cnpm i dayjs --save
import dayjs from 'dayjs'const filterTimes = (val,format='YYYY-MM-DD') =>{if(!isNull(val)){val = parseInt(val)*1000return dayjs(val).format(format)}else{return '--'}
}export const isNull = (val)=>{if(!val) return trueif(JSON.stringify(val)==="{}") return trueif(JSON.stringify(val)==="[]") return true
}export default (app)=>{app.config.globalProperties.$filters={filterTimes}
}
import filters from './utils/filters'
filters(app)
{{$filters.filterTimes(row.create_time)}}