應(yīng)用場(chǎng)景:
?
一般管理端的菜單欄是根據(jù)登錄用戶角色不同,動(dòng)態(tài)生成的,在vue中我們不止菜單欄需要?jiǎng)討B(tài)生成,同時(shí)我們路由也需要?jiǎng)討B(tài)生成。
使用到的組件:
組件名稱 | 組件版本 | 組件作用 |
axios | 1.3.4 | 用于發(fā)送請(qǐng)求獲取數(shù)據(jù) |
element-ui | 2.15.13 | 前端ui組件庫(kù),制作頁(yè)面使用 |
vue-router | 3.5.1 | vue路由組件 |
vuex | 3.6.2 | 路由狀態(tài)管理 |
mockjs | 1.1.0 | 模擬后臺(tái)返回?cái)?shù)據(jù)接口 |
代碼實(shí)現(xiàn):
項(xiàng)目結(jié)構(gòu):
axios相關(guān)代碼
http.js
import axios from 'axios'
import router from '@/router/index.js'
import ElementUI from 'element-ui'
const request = axios.create({
// baseURL: 'http://192.168.1.150:8888/'
headers: {
'Content-Type': 'application/json;charset=utf-8'
}
})
request.interceptors.response.use(config => {
const code = config.data.code
if (code === 200) {
return config
} else {
ElementUI.Message('服務(wù)忙')
return Promise.reject
}
}, error => {
switch (error.response.status) {
case 404:
router.push('/404')
break
case 500:
ElementUI.Message('error')
}
return Promise.reject
})
export default request
login.js
import request from '@/utils/http.js'
/**
* 獲取驗(yàn)證碼
*/
export function code () {
return request({
url: '/getCode',
method: 'get'
})
}
/**
* 登錄
*/
export function loginAPI (loginInfo) {
return request({
url: '/login',
method: 'post',
data: {
loginInfo
}
})
}
/**
* 獲取菜單
*/
export function getMenu () {
return request({
url: '/sys/getMenu',
method: 'post'
})
}
mock.js
import Mock from 'mockjs'
const Random = Mock.Random
const R = {
code: 200,
message: '執(zhí)行成功',
data: null
}
// 獲取驗(yàn)證碼
Mock.mock('/getCode', 'get', () => {
R.data = {
token: Random.string(32),
imageUri: Random.dataImage('100x100', '8878')
}
return R
})
// 登錄
Mock.mock('/login', 'post', (value) => {
console.log(value)
R.data = {
token: 'token'
}
return R
})
// 獲取菜單欄信息
Mock.mock('/sys/getMenu', 'post', () => {
const menu = [{
id: 1,
title: '系統(tǒng)設(shè)置',
name: 'settings',
path: '/sys',
icon: 'el-icon-setting',
component: '/sys/SysSettings.vue',
children: [
{
id: 2,
title: '用戶管理',
name: 'user',
path: '/sys/user',
component: '/sys/UserInfo.vue',
icon: 'el-icon-user'
},
{
id: 3,
title: '角色管理',
name: 'role',
path: '/sys/role',
component: '/sys/RoleInfo.vue',
icon: 'el-icon-menu'
},
{
id: 4,
title: '權(quán)限管理',
name: 'menu',
path: '/sys/menu',
component: '/sys/MenuInfo.vue',
icon: 'el-icon-menu'
}
]
}]
const role = []
R.data = { menu, role }
return R
})
vuex相關(guān)代碼
menu.js
import Vue from 'vue'
import Vuex from 'vuex'
import { getMenu } from '@/api/login.js'
Vue.use(Vuex)
export default {
state: {
menuList: [],
flag: false
},
getters: {
},
mutations: {
updateMenu (state, value) {
state.menuList = value
},
updateFlag (state, value) {
state.flag = value
}
},
actions: {
async httpGetMenu (store) {
const { data } = await getMenu()
store.commit('updateMenu', data.data.menu)
store.commit('updateFlag', true)
}
}
}
index.js
import Vue from 'vue'
import Vuex from 'vuex'
import menu from '@/store/modules/Menu.js'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
token: ''
},
getters: {
},
mutations: {
updateToken (state, value) {
state.token = value
localStorage.setItem('token', value)
},
restState (stste) {
stste.token = ''
}
},
actions: {
},
modules: {
namespaced: true,
menu
}
})
vue-router文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-828484.html
import Vue from 'vue'
import VueRouter from 'vue-router'
import UserLogin from '@/views/UserLogin.vue'
import HomeIndex from '@/views/HomeIndex.vue'
import store from '@/store/index.js'
Vue.use(VueRouter)
const routes = [
{
path: '/',
redirect: '/login'
},
{
path: '/login',
name: 'login',
component: UserLogin
},
{
path: '/home',
name: 'home',
component: HomeIndex,
children: []
}
]
const router = new VueRouter({
routes
})
router.beforeEach(async (to, form, next) => {
const flag = store.state.menu.flag
if (!flag) {
await store.dispatch('httpGetMenu')
const menuList = store.state.menu.menuList
for (const i in menuList) {
if (menuList[i].children) {
for (let j = 0; j < menuList[i].children.length; j++) {
const childrenMenu = {
path: menuList[i].children[j].path,
name: menuList[i].children[j].name,
children: []
}
childrenMenu.component = () => import('@/views' + menuList[i].children[j].component)
router.addRoute('home', childrenMenu)
next({ ...to, replace: true })
}
}
}
}
next()
})
// 獲取原型對(duì)象push函數(shù)
const originalPush = VueRouter.prototype.push
// 獲取原型對(duì)象replace函數(shù)
const originalReplace = VueRouter.prototype.replace
// 修改原型對(duì)象中的push函數(shù)
VueRouter.prototype.push = function push (location) {
return originalPush.call(this, location).catch(err => err)
}
// 修改原型對(duì)象中的replace函數(shù)
VueRouter.prototype.replace = function replace (location) {
return originalReplace.call(this, location).catch(err => err)
}
export default router
home.vue文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-828484.html
<template>
<div class="home_root">
<el-container>
<el-header
>物業(yè)管理
<el-dropdown :hide-on-click="false">
<span class="el-dropdown-link">
設(shè)置<i class="el-icon-arrow-down el-icon--right"></i>
</span>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item>個(gè)人中心</el-dropdown-item>
<el-dropdown-item @click.native="loginOut()">退出</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</el-header>
<el-container>
<el-aside width="200px">
<el-col>
<el-menu
default-active="2"
class="el-menu-vertical-demo"
background-color="#545c64"
text-color="#fff"
active-text-color="#ffd04b"
>
<el-submenu
:index="menu.id + ''"
v-for="menu in menuList"
:key="menu.id"
>
<template slot="title">
<i :class="menu.icon"></i>
<span>{{ menu.title }}</span>
</template>
<el-menu-item
:index="children.id + ''"
v-for="children in menu.children"
:key="children.id"
>
<template slot="title">
<div @click="openUri(children.path)">
<i :class="children.icon"></i>
<span slot="title">{{ children.title }}</span>
</div>
</template>
</el-menu-item>
</el-submenu>
</el-menu>
</el-col>
</el-aside>
<el-main>
<router-view></router-view>
</el-main>
</el-container>
</el-container>
</div>
</template>
<script>
export default {
computed: {
menuList () {
return this.$store.state.menu.menuList
}
},
methods: {
handleClose (tag) {
this.tags.splice(this.tags.indexOf(tag), 1)
},
openUri (uri) {
this.$router.push(uri)
},
loginOut () {
console.log('loginOut')
localStorage.clear()
sessionStorage.clear()
this.$store.commit('restState')
this.$router.push('/')
}
}
}
</script>
<style lang="less" scoped>
.home_root {
padding: 0;
margin: 0;
height: 100vh;
}
.el-container {
padding: 0;
margin: 0;
height: 100%;
}
.el-header,
.el-footer {
background-color: #b3c0d1;
color: #333;
text-align: center;
line-height: 60px;
}
.el-aside {
background-color: #d3dce6;
color: #333;
text-align: center;
line-height: 200px;
}
.el-main {
background-color: #e9eef3;
color: #333;
// text-align: center;
// line-height: 160px;
padding: 0;
}
body > .el-container {
margin-bottom: 40px;
}
.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
line-height: 260px;
}
.el-container:nth-child(7) .el-aside {
line-height: 320px;
}
.el-dropdown {
float: right;
}
.el-dropdown-link {
cursor: pointer;
color: #060606;
}
.el-icon-arrow-down {
font-size: 12px;
}
.el-col-24 {
height: 100%;
.el-menu {
height: 100%;
}
}
</style>
到了這里,關(guān)于Vue2動(dòng)態(tài)路由的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!