013Vue3独立路由模块加载使用范例

013Vue3独立路由模块加载使用范例

  • 通过路由懒加载,隔离辅助模块与主模块,也支持辅助模块独立调试与运行

子路由文件与模块map

export default [
  {
    path: '',
    alias: '/about',
    name: 'about',
    component: () => import('@/views/test/about/Index.vue'),
    meta: {
      title: '关于我们',
    },
  },
  {
    path: 'props',
    name: 'props',
    component: () => import('@/views/test/props/Index.vue'),
    meta: {
      title: 'props测试',
    },
  },
];

主Vue3文件入口

  • web-client\ui\src\views\test\Index.vue
<script setup lang="ts">
import IndexTest from './routes';

const menus = IndexTest.map((el: any) => {
  return {
    path: '/test/' + el.path,
    title: el.meta.title || el.name || el.path,
  };
});
</script>

<template>
  <div>
    <el-link type="primary" v-for="item in menus" :key="item.path" :href="item.path">
      {{ item.title }}
    </el-link>
  </div>
  <router-view></router-view>
</template>

<style scoped lang="scss">
.el-link {
  margin-right: 8px;
}
</style>

独立路由模块挂载范例

// 独立模块加载
import IndexTest from '@/views/test/Index.vue'; // 路由router-view
import testRoutes from '@/views/test/routes'; // 子路由文件

// 定义项目路由
const routes: RouteRecordRaw[] = [
  {
    path: '/', // 主路由地址
    alias: '/index', // 别名,和路由一样
    name: 'index',
    component: IndexView, // 组件加载
    meta: {
      // meta中可以定义一些页面的元素
      title: '主页', // 页面的标题
    },
    children: [
      // 路由嵌套,拼接主路由与当前子路由,如/index+ui组合成/index/ui
      {
        path: '',
        alias: 'ui',
        name: 'ui',
        component: () => import('@/views/ui/Index.vue'), // 组件懒加载
        meta: {
          title: 'UI主页',
        },
      },
    ],
  },
  {
    path: '/test', // 主路由地址
    alias: '/t', // 别名,和路由一样
    name: 'testIndex',
    component: IndexTest, // 组件加载
    meta: {
      title: '测试', // 页面的标题
    },
    // 这里将独立的路由模块挂载在指定的路由下
    children: [...testRoutes],
  },
];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿赛工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值