vue实现动态路由

首页创建一个文件夹名为router,在下面创建一个index.js文件具体如下:

import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from "@/views/Main";
import Login from "@/views/Login";
import store from "@/store"
Vue.use(VueRouter);

const routes = [
    // 加载主页
    {path: '/', component: Main, redirect: '/home', name: "main", children: [], replace: true},
    // 登录
    {path: '/login', name: 'login', component: Login}

]


const router = new VueRouter({
    routes,
})


//  动态路由核心代码

const list = store.state.tab.getList;
if (list !== null && list.length > 0) {
    list.forEach((item) => {
        router.addRoute('main', {
            path: item.name,
            name: item.name,
            component: () => import("@/views/" + item.component)
        })
    })
}


// router.addRoute('main', {path: 'home', name: 'home', component: () => import("@/views/Home")})
// router.addRoute('main', {path: 'user', name: 'user', component: () => import("@/views/User")})
// router.addRoute('main', {path: 'mall', name: 'mall', component: () => import("@/views/Mall")})

export default router

一般前端登录后,后端会返回路由的数据

vuex刷新页面数据会丢失,所以最好存在localStorage中,返回会的是数组,最好转换成字符串JSON.stringify()方法就可以。

我在vuex中直接就能取

我只需要把数据利用循环添加到addRoute()方法中就可以实现

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现动态路由菜单,可以按照以下步骤进行: 1. 在路由配置文件中,定义一个需要动态生成的路由数组,如下所示: ``` const dynamicRoutes = [ { path: '/dashboard', name: 'Dashboard', component: () => import('@/views/dashboard'), meta: { title: 'Dashboard', icon: 'dashboard' } }, { path: '/user', name: 'User', component: () => import('@/views/user'), meta: { title: 'User', icon: 'user' }, children: [ { path: 'list', name: 'UserList', component: () => import('@/views/user/list'), meta: { title: 'User List', icon: 'user' } }, { path: 'detail/:id', name: 'UserDetail', component: () => import('@/views/user/detail'), meta: { title: 'User Detail', icon: 'user' } } ] } ] ``` 2. 在路由配置文件中,定义一个路由表,包含所有静态路由和需要动态生成的路由,如下所示: ``` import Vue from 'vue' import Router from 'vue-router' import staticRoutes from './staticRoutes' import dynamicRoutes from './dynamicRoutes' Vue.use(Router) const router = new Router({ mode: 'history', base: process.env.BASE_URL, routes: [ ...staticRoutes, ...dynamicRoutes ] }) export default router ``` 3. 在菜单组件中,根据路由动态生成菜单,如下所示: ``` <template> <el-menu :default-active="$route.path" class="el-menu-vertical-demo" :collapse="isCollapse"> <template v-for="item in menuList"> <template v-if="item.children"> <el-submenu :index="item.path"> <template slot="title"> <i :class="item.meta.icon"></i> <span slot="title">{{ item.meta.title }}</span> </template> <template v-for="child in item.children"> <el-menu-item :index="child.path" :key="child.path"> <router-link :to="child.path">{{ child.meta.title }}</router-link> </el-menu-item> </template> </el-submenu> </template> <template v-else> <el-menu-item :index="item.path" :key="item.path"> <i :class="item.meta.icon"></i> <router-link :to="item.path">{{ item.meta.title }}</router-link> </el-menu-item> </template> </template> </el-menu> </template> <script> export default { data() { return { menuList: [] } }, created() { this.menuList = this.$router.options.routes.filter(route => route.meta && route.meta.title) } } </script> ``` 其中,menuList 是根据路由表生成的菜单数组。在 created 钩子中,我们通过过滤路由表中有 meta.title 属性的路由,来生成菜单数组。 这样,就可以实现动态路由菜单了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值