vue3根据后台的地址动态显示路由

1.router.js 正常路由那种写法, 我这里路由是放在index的子路由里了

import { createRouter, createWebHashHistory } from "vue-router"
export const constRoutes = [
  {
    path: '/',
    name: 'index',
    meta: {
      title: "首页"
    },
    component: () => import('@/views/index/index.vue'),
    children: [],
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/LoginPage/loginPage.vue')
  },
  {
    path: '/test',
    name: '测试',
    component: () => import('@/views/test.vue')
  }
]

export const router = createRouter({
  history: createWebHashHistory(),
  routes: constRoutes
})

export default router

2.在router目录下新建map.js,将后台传的地址改成前端能识别的

let map = new Map();
map.set("configManagement", ()=>import('@/views/configManagement/IndexHome.vue')) //管理
let actionRouter = []
menus.map(item => {
      let component=item.accessUrl.replace("/","")
      actionRouter.push({
          path: item.accessUrl,
            name: item.name,
            component:map.get(component),
            meta: {
              title: item.name,
              icon: require("@/assets/icons/homeIcon.png"),
            },
            children: []
        })
    })
    return actionRouter;
}

3.登录后生成路由

                    let arr = getRouters($store.state.menu); 
                    $store.commit("routerArr",arr) //这里保存处理好的路由,用来生成左侧菜单栏
                    arr.forEach((item) => {
                        router.addRoute('index',item);
                    });

4.在app.vue里放置路由守卫,放置刷新后屏幕变白

   const method = {
            reloadRoute() {
                console.log($store.state.menu.length)
                if ($store.state.menu.length > 0) {
                    let arr = getRouters($store.state.menu);
                    arr.forEach((item) => {
                        router.addRoute("index", item);
                    });
                    router.push({   //这里因为访问的是子路由,不这么写找不到子路由的父级
                        path: "/",
                    });
                } else {
                    router.push({
                        path: "/login",
                    });
                }
            },
        };
        onBeforeMount(() => {
            method.reloadRoute();
        });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 2中创建动态路由的方法如下: 1. 首先,在你的项目中创建一个名为`routes.js`(或者其他你喜欢的名字)的文件。 2. 在`routes.js`文件中,定义你的动态路由。你可以使用Vue Router的`component`字段来指定每个路由对应的组件。例如: ```javascript import Home from './components/Home.vue' import About from './components/About.vue' import NotFound from './components/NotFound.vue' const routes = [ { path: '/', name: 'home', component: Home }, { path: '/about', name: 'about', component: About }, // 其他动态路由 { path: '*', name: 'not-found', component: NotFound } ] export default routes ``` 3. 在你的主文件(通常是`main.js`)中,导入`routes.js`并使用它来配置Vue Router。你需要通过`new VueRouter({})`创建一个新的Vue Router实例,并将其传递给Vue实例的`router`选项。例如: ```javascript import Vue from 'vue' import VueRouter from 'vue-router' import routes from './routes.js' Vue.use(VueRouter) const router = new VueRouter({ mode: 'history', routes }) new Vue({ router, render: h => h(App) }).$mount('#app') ``` 4. 现在,你可以在Vue组件中使用`<router-link>`和`<router-view>`来导航和显示动态路由。例如,在你的导航栏组件中,你可以使用`<router-link>`来创建链接到不同路由的导航按钮: ```html <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> ``` 并在你的主应用程序组件中使用`<router-view>`来显示当前路由的组件内容: ```html <router-view></router-view> ``` 这样,当用户点击导航按钮时,Vue Router将根据路由配置自动加载相应的组件并显示在`<router-view>`中。 这是一个简单的示例,你可以根据你自己的需求和项目结构进行修改和扩展。希望这能帮助到你!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值