vue3使用vue-router嵌套路由(多级路由)

Vue3嵌套路由详解:实例化与功能优化
本文详细介绍了Vue3中嵌套路由的使用,包括如何通过createApp()方法实例化,以及新增的metaprop和routeprop特性。建议按功能模块分层,控制层级和路由数量以提高管理效率。

Vue3 嵌套路由的使用和 Vue2 相差不大,主要的区别是 Vue3 的路由实例化使用了 createApp() 方法,所以实例化路由时需要传入根组件。另外,Vue3 的路由对象除了包含 Vue2 中的导航守卫、导航钩子和解析守卫等功能外,还新增了 meta prop 和 route prop。

在使用嵌套路由时,建议将路由按照功能模块进行分层,每一层代表一个主要的功能块,每个层级下的路由数量不要过多,一般建议不要超过 10 个,层级也不要超过 5 层。同时,根据实际业务需求,可以适当调整路由层级和数量,以达到更好的管理和使用效果。

route文件下 index.ts文件代码:

import {
  createRouter,
  createWebHashHistory,
  RouteRecordRaw
} from "vue-router";
// createWebHashHistory,createWebHistory

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/views/Home/index.vue'),
    meta: {
      title: 'Home Page',
      roles: ['admin', 'admin1']
    },
    children: [
      {
        path: 'lx',
        name: 'Lx',
        component: () => import('@/views/Home/Lx.vue'),
        // 也可以使用props传参方式接收传来的参数
        props: (propsRouter) => {
          // console.log('props >router', propsRouter)
          // 可以return query 也可以return params支持两种传参方式
          return propsRouter.query
        },
        // 多级嵌套 建议用query传参
        children: [
          {
            path: 'childA',
            name: 'ChildA',
            component: () => import('@/views/Home/ChildA.vue'),
          },
        ]
      },
      {
        path: 'lxb/:id/:title', // 提前定义params参数(可以定义多个)
        name: 'Lxb',
        component: () => import('@/views/Home/Lxb.vue'),
      },
    ]
  },
]

export const router = createRouter({
  // 路由的history模式,共有三种模式,
  // createWebHistory、createWebHashHistory、createMemoryHistory
  history: createWebHashHistory(),// history: createWebHistory(),
  // 路由配置
  routes,
  // 是否严格匹配路由
  strict: true,
  // 路由跳转完成后,页面滚动行为
  scrollBehavior: () =>
Vue.js 应用中,`vue-router` 提供了强大的路由功能,其中嵌套路由多级路由)是实现复杂页面结构的关键。嵌套路由允许将一个路由组件嵌套在另一个路由组件中,从而构建出具有父子关系的页面结构。 ### 实现嵌套路由的方法 嵌套路由的核心在于使用 `children` 属性来定义子路由。主路由通过 `path` 定义,子路由则在主路由的 `children` 数组中进行配置。 以下是一个典型的嵌套路由配置示例: ```javascript import Vue from &#39;vue&#39; import VueRouter from &#39;vue-router&#39; Vue.use(VueRouter) // 引入页面组件 const Home = () => import(&#39;../views/Home.vue&#39;) const News = () => import(&#39;../views/News.vue&#39;) const Message = () => import(&#39;../views/Message.vue&#39;) const About = () => import(&#39;../views/About.vue&#39;) // 创建并导出路由实例 export default new VueRouter({ routes: [ { path: &#39;/about&#39;, component: About }, { path: &#39;/home&#39;, component: Home, children: [ { path: &#39;news&#39;, // 子路由路径不加斜杠,表示在 /home/ 后追加 component: News }, { path: &#39;message&#39;, component: Message } ] } ] }) ``` ### 使用 `<router-view>` 渲染子路由 在父级路由组件中,需要使用 `<router-view>` 来渲染子路由内容。例如,在 `Home.vue` 中: ```vue <template> <div> <h1>Home 页面</h1> <router-view></router-view> <!--路由内容将在此处渲染 --> </div> </template> ``` ### 路由跳转配置 在页面中使用 `<router-link>` 组件进行路由跳转时,路径应包含父级路径。例如: ```vue <template> <div> <router-link to="/home/news">前往 News</router-link> <router-link to="/home/message">前往 Message</router-link> </div> </template> ``` ### 路由重定向配置 可以通过 `redirect` 属性实现默认子路由跳转,例如访问 `/home` 时自动跳转到 `/home/news`: ```javascript { path: &#39;/home&#39;, component: Home, redirect: &#39;/home/news&#39;, // 默认跳转到 news 页面 children: [ { path: &#39;news&#39;, component: News }, { path: &#39;message&#39;, component: Message } ] } ``` ### 路由懒加载 在大型应用中,为了提高性能,可以使用路由懒加载功能,按需加载组件。例如: ```javascript const News = () => import(&#39;../views/News.vue&#39;) ``` 这种方式确保组件只在需要时才被加载,减少初始加载时间。 ### 嵌套路由的注意事项 -路由的 `path` 不应以 `/` 开头,否则会被当作根路径。 - 确保父级组件中包含 `<router-view>`,以便渲染子路由- 使用 `redirect` 可以实现默认子路由跳转,提升用户体验。 通过上述配置,可以轻松实现多级路由结构,适用于复杂的前端应用需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值