动态路由使用

目录

添加动态路由

添加单个路由

添加多个路由

指定路由添加的位置

导航守卫添加路由

删除动态路由


添加动态路由

添加动态路由的方法有两个:addRoutes(添加多个路由)和addRoute(添加单个路由),但是addRoutes只能vue2用,并且已经过时了,所以推荐用addRoute

添加单个路由
  • getRoutes()  可以查看所有路由

  • hasRoute()   可以查看是否有某路由

<template>
  <main>
    <el-button @click="addRoutes">添加动态路由</el-button>
    <el-button @click="toDemo01">跳转Demo01</el-button>
  </main>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'

const r = useRouter()

const addRoutes = () => {
  r.addRoute({
    path: '/demo01',
    name: 'demo01',
    component: () => import('@/views/Demo01.vue'),
  })
  
  // 查看所有路由
  console.log(r.getRoutes());
  // 查看是否存在某路由
  console.log(r.hasRoute('demo01'));
}

const toDemo01 = () => {
  r.push('/demo01')
}
</script>

还可以添加嵌套路由

  • 有一点需要注意:路由规则配置文件里,子路由可以不写前面的父路由路径,但是动态路由里不行,必须写完整路径
<template>
  <main>
    <h2>Home</h2>
    <el-button @click="addRoutes">添加动态路由</el-button>
    <el-button @click="toDemo02">跳转Demo02</el-button>
  </main>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'

const r = useRouter()

const addRoutes = () => {
  r.addRoute({
    path: '/demo01',
    name: 'demo01',
    children: [
      {
        path: '/demo01/demo02', // 子路由一定要完整 只写/demo02会报错查找不到路由
        name: 'demo02',
        component: () => import('@/views/Demo02.vue'),
      }
    ]
  })

  // 查看所有路由
  console.log(r.getRoutes());
  // 查看是否存在某路由
  console.log(r.hasRoute('demo02'));
}

const toDemo02 = () => {
  r.push('/demo01/demo02')
}
</script>

还可以添加动态路由

<template>
  <main>
    <el-button @click="addRoutes">添加动态路由</el-button>
    <el-button @click="toDemo01">跳转Demo01</el-button>
  </main>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'

const r = useRouter()

const addRoutes = () => {
  r.addRoute({
    path: '/demo01/:id', // 还可以添加动态路由  路由只要是  ” /demo01/任意路由 “  都可以匹配到Demo01组件
    name: 'demo01',
    component: () => import('@/views/Demo01.vue'),
  })

  // 查看所有路由
  console.log(r.getRoutes());
  // 查看是否存在某路由
  console.log(r.hasRoute('demo01'));
}

const toDemo01 = () => {
  // r.push('/demo01/1')
  r.push('/demo01/100')
}
</script>
添加多个路由

添加多个动态路由,可以把所有路由对象组成一个数组,对数组进行遍历,然后挨个添加

<template>
  <main>
    <el-button @click="addRoutes">添加动态路由</el-button>
    <el-button @click="toDemo01">跳转Demo01</el-button>
    <el-button @click="toDemo02">跳转Demo02</el-button>
  </main>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import Demo01 from '@/views/Demo01.vue'
import Demo02 from '@/views/Demo02.vue'

const r = useRouter()

const addRoutes = () => {
  // 路由信息组成的数组
  const routeList = [
    {
      path: '/demo01',
      name: 'demo01',
      component: Demo01
    },
    {
      path: '/demo02',
      name: 'demo02',
      component: Demo02
    },
  ]

  // 只能遍历挨个添加
  routeList.forEach((item: any) => r.addRoute(item))

  // 查看所有路由
  console.log(r.getRoutes());
  // 查看是否存在某路由
  console.log(r.hasRoute('demo01'));
  console.log(r.hasRoute('demo02'));
}

const toDemo01 = () => {
  r.push('/demo01')
}

const toDemo02 = () => {
  r.push('/demo02')
}
</script>
指定路由添加的位置
  • 也就是可以指定路由添加到那个路由下面,addRoute第一个参数指定要添加到那,第二个参数正常的写路由信息

路由配置文件

import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue')
    }
  ]
})

export default router

我们希望路由添加到about路由里面,成为about的子路由

<template>
  <main>
    <el-button @click="addRoutes">添加动态路由</el-button>
    <el-button @click="toDemo01">跳转Demo01</el-button>
  </main>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'

const r = useRouter()

const addRoutes = () => {
  r.addRoute('about', {
    path: '/about/demo01', // 这里路由一定要写完整。路由规则配置文件里,子路由可以不写前面的路由路径,但是动态路由里不行,必须写完整路径
    name: 'demo01',
    component: () => import('@/views/Demo01.vue'),
  })

  // 查看所有路由
  console.log(r.getRoutes());
  // 查看是否存在某路由
  console.log(r.hasRoute('demo01'));
}

const toDemo01 = () => {
  r.push('/about/demo01')
}
</script>
导航守卫添加路由

一般做前端权限的时候用,添加后应该通过返回新的位置来触发重定向,不然地址栏输入路由不会跳转

router.beforeEach(to => {
  if (!hasNecessaryRoute(to)) {
    router.addRoute(generateRoute(to))
    // 触发重定向
    return to.fullPath
  }
})
  • hasNecessaryRoute(to)这个方法是避免重定向的,函数结果返回一个布尔值就行了。to参数携带了要前往路由的具体信息,如果是已经添加过的动态路由,应该返回false,避免重复添加,造成路由重定向,如果没有添加过,则应该返回true,进行动态路由的添加

举个例子,hasNecessaryRoute函数可以这样写

const hasNecessaryRoute = (to: any) => {
  // return router.getRoutes().some(route => route.path === to.path)
  // 或者下面这样
  return router.hasRoute(to.name)
}

前端权限思想

只是大致写了下思路,方法千奇百怪,能实现就行

1. 点击登录的时候,调用接口,后端返回此用户的路由信息,

2. 跳转路由到首页,然后触发全局前置导航守卫

3. 在全局前置导航守卫中进行动态路由的添加

删除动态路由

通过添加一个名称冲突的路由。如果添加与现有途径名称相同的途径,会先删除路由,再添加路由
 

router.addRoute({ path: '/about', name: 'about', component: About })
// 这将会删除之前已经添加的路由,因为他们具有相同的名字且名字必须是唯一的
router.addRoute({ path: '/other', name: 'about', component: Other })

通过使用 router.removeRoute() 按名称删除路由

router.addRoute({ path: '/about', name: 'about', component: About })
// 删除路由
router.removeRoute('about')

注意:当路由被删除时,所有的别名和子路由也会被同时删除

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
路由的children配置是用来定义子路由的。在VueRouter的参数中使用children配置来添加子路由。需要注意的是,在children中,子路由的路径不需要加斜杠/。 如果想要直接显示儿子路由或者女儿路由,可以在路由配置文件中使用redirect重定向来实现。具体的配置代码如下所示: ```javascript children: [ { path:"", redirect:"routerboy" }, { path:"routerboy", name:"routerboy", component:routerboy }, { path:"routergirl", name:"routergirl", component:routergirl } ] ``` 在主路由页面routerindex的代码中,首先要写入<router-view></router-view>来添加路由视图。然后使用router-link并写上子路由的位置路径,注意,这里需要写上完整的路径。例如: ```html <template> <div> <h3>路由首页</h3> <router-link to="/routerproject/routerindex/routerboy">跳转至儿子路由页面</router-link> <router-link to="/routerproject/routerindex/routergirl">跳转至女儿路由页面</router-link> <router-view></router-view> </div> </template> ``` 以上是关于路由的children配置和使用的一些说明和示例代码。通过配置和使用子路由,可以实现在Vue应用中进行页面之间的切换和导航。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span> #### 引用[.reference_title] - *1* *2* *3* *4* [Vue --- router,路由嵌套children的基本使用](https://blog.csdn.net/qq_21980517/article/details/100073887)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值