vue3 + vite 实现一个动态路由加载功能

假设后端返回的格式是这样子

{
  "menu": [
    {
      "path": "/admin",
      "name": "adminLayout",
      "redirect": "/admin/index",
      "componentPath": "/layout/admin/index.vue",
      "children": [
        {
          "path": "index",
          "name": "Index",
          "meta": {
            "title": "首页",
            "keepAlive": true,
            "requireAuth": true
          },
          "componentPath": "/views/index/index.vue"
        },
        {
          "path": "logic-flow",
          "name": "LogicFlow",
          "meta": {
            "title": "可视化拖拽",
            "keepAlive": true,
            "requireAuth": true
          },
          "componentPath": "/views/logic-flow/index.vue"
        },
        {
          "path": "custom-form",
          "name": "CustomForm",
          "meta": {
            "title": "自定义表单",
            "keepAlive": true,
            "requireAuth": true
          },
          "componentPath": "/views/custom-form/index.vue"
        },
        {
          "path": "big-screen",
          "name": "BigScreen",
          "meta": {
            "title": "可视化大屏",
            "keepAlive": true,
            "requireAuth": true
          },
          "componentPath": "/views/big-screen/index.vue"
        },
        {
          "path": "d3",
          "name": "D3",
          "meta": {
            "title": "D3",
            "keepAlive": true,
            "requireAuth": true
          },
          "componentPath": "/views/d3/index.vue"
        },
        {
          "path": "konva",
          "name": "Konva",
          "meta": {
            "title": "Konva",
            "keepAlive": true,
            "requireAuth": true
          },
          "componentPath": "/views/konva/index.vue"
        },
        {
          "path": "/function",
          "name": "Function",
          "redirect": "/function/index",
          "children": [
            {
              "path": "large-file-upload",
              "name": "LargeFileUpload",
              "meta": {
                "title": "LargeFileUpload",
                "keepAlive": true,
                "requireAuth": true
              },
              "componentPath": "/views/function/large-file-upload/index.vue"
            },
            {
              "path": "virtual-list",
              "name": "VirtualList",
              "meta": {
                "title": "VirtualList",
                "keepAlive": true,
                "requireAuth": true
              },
              "componentPath": "/views/function/virtual-list/index.vue"
            }
          ]
        }
      ]
    },
    {
      "path": "/data-chart",
      "name": "DataChart",
      "meta": {},
      "componentPath": "/views/big-screen/data-chart.vue"
    }
  ]
}

在vite里主要使用到的方法是import.meta.glob,它能通过我们提供的路径动态引入相关的组件,如果我们传入了../views/**/**.vue,它返回的相关格式是这样子,这样的话就可以用过路径获取相关异步导入组件的函数了

完整代码如下(permission.ts):

import router from './index'
import { RouteRecordRaw } from 'vue-router'
import { useUserStore } from '@/store/modules/user'

const viewsModules = import.meta.glob('../views/**/**.vue')
const layoutModules = import.meta.glob('../layout/*/*.vue')

// 这个方法主要就是将componentPath转成异步引入函数component
const menuToRoutes = (menus: RouteRecordRaw[]) => {
  if (!menus || !menus.length) {
    return []
  }
  const routes: RouteRecordRaw[] = []
  menus.forEach((item: any) => {
    const meta = Object.assign({}, item.meta)
    let component
    if (item.componentPath) {
      // 如果找不到就给个404的组件
      component =
        viewsModules[`..${item.componentPath}`] ||
        layoutModules[`..${item.componentPath}`] ||
        viewsModules['../views/404/index.vue']
    }
    routes.push({
      meta,
      name: item.path,
      path: item.path,
      component: component,
      redirect: item.redirect,
      children: menuToRoutes(item.children),
    })
  })
  return routes
}

export const setupPermission = () => {
  router.beforeEach((to, from, next) => {
    const userStore = useUserStore()
    if (!userStore.menu.length) {
      // 获取路由
      // userStore.menu就是json里的menu字段
      userStore
        .setMenu()
        .then(() => {
          // 动态路由注册
          router.addRoute({
            path: '/',
            redirect: '/admin',
            children: menuToRoutes(userStore.menu),
          })
          next({ ...to, replace: true })
        })
        .catch(() => {
          next()
        })
    } else {
      next()
    }
  })
}

最后在main.js里调用一下setupPermission方法就行~

PS: 如果退出登录需要清除动态路由的话,因为现在vue-router没有提供可以直接清空的方法,所以可以考虑返回登录页后刷新一下界面来解决~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
动态路由可以使用 Vue Router 提供的 `async` 函数来实现。在 Vue Router 中,我们可以通过 `routes` 配置项来配置路由,而 `routes` 可以是一个数组,也可以是一个返回数组的函数。这个函数可以异步路由配置,比如从服务器获取路由配置信息。 下面是一个简单的例子: ```javascript const router = createRouter({ routes: [ { path: '/', name: 'Home', component: () => import('@/views/Home.vue') }, { path: '/about', name: 'About', component: () => import('@/views/About.vue') }, { path: '/dynamic', name: 'Dynamic', component: () => import('@/views/Dynamic.vue') }, // ... ] }) ``` 在这个例子中,我们使用 `import()` 函数来异步组件。当用户访问 `/dynamic` 路径时,这个组件才会被动态。 为了本地路由和线上路由匹配,我们可以使用环境变量来判断当前是否处于线上环境。比如我们可以在 `.env` 文件中定义一个环境变量 `VUE_APP_ENV`: ```env VUE_APP_ENV=production ``` 然后在代码中使用 `process.env.VUE_APP_ENV` 来获取当前环境变量的值。根据不同的值,我们可以不同的路由配置。 下面是一个示例代码: ```javascript const routes = [ { path: '/', name: 'Home', component: () => import('@/views/Home.vue') }, { path: '/about', name: 'About', component: () => import('@/views/About.vue') }, // ... ] if (process.env.VUE_APP_ENV === 'production') { // 在线上环境动态路由 import('@/router/routes.production').then(routes => { router.addRoutes(routes.default) }) } else { // 在本地环境直接使用静态路由 router.addRoutes(routes) } ``` 在上面的代码中,我们先定义了一个静态的路由配置 `routes`,然后根据环境变量的值来不同的路由配置。在线上环境下,我们使用 `import()` 函数异步一个 `routes.production.js` 文件,该文件返回一个路由配置数组;在本地环境下,我们直接使用静态路由配置。最后使用 `router.addRoutes()` 函数将动态路由路由中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值