• Vue3 之 Vue - Router


    目录

    一、历史进程

    1. 后端路由阶段

    2. 前后端分离阶段

    3. 单页面富应用(SPA)

    二、路由跳转的方式

    1. URL的hash

    2. HTML5的History

    3. 区别

    三、Vue-Router基本使用

    1. 概念

    2. 安装

    3. 路由的使用步骤 

    01 - 创建映射组件

    02 - 创建路由对象 

    03 - 在 main.js 中导入

    04 - 在App.vue文件中设置

    05 - 效果 

    4. 路由的默认路径 

    5. 使用history模式 

    6. router路由的属性

    7. notFound

    01 - 路由设置

    02 - 组件代码 

    03 - 效果 

    8. router-link 

    01 - to属性

    02 - replace属性

    03 - active-class属性

            默认

            设置

    04 - exact-active-class属性

    四、路由懒加载分包处理

    1. 路由懒加载

    2. 分包处理

    五、动态路由

    1. 设置路由

    2. 跳转设置

    3. 组件中获取值

    4. 效果 

    六、路由嵌套

    1. 子组件创建

    2. 路由设置

    3. 组件设置

    4. 效果 

    5. exact-active-class属性

    七、路由的编程式导航,代码的页面跳转

    1. 简单使用

    代码

    效果

    2. 跳转方式

    01 - 通过 name 跳转并传递参数

            设置

            传递

            获取

            效果

            缺点

    02 - 通过 path 跳转并传递参数

            设置

            传递

            获取

            效果

    3. 前进后退

    back

    forward

    go

    4. push | replace

    01 - push

    02 - replace

    八、动态管理路由对象

    1. 添加路由

    01 - 添加顶层路由

    02 - 添加次级路由

    03 - 添加三级路由

    2. 删除路由

    01 - 方式一

    02 - 方式二

    03 - 方式三

    3. 检查路由

    4. 获取所有路由

    九、路由导航守卫钩子

    1. 全局的前置守卫 

    01 - 参数

    02 - 返回值 

    03 - 登录守卫功能栗子 🌰

            router.js代码

            login组件代码

            效果

    2. 其他的导航守卫 导航守卫 | Vue Router

    3. 完整的导航解析流程


    一、历史进程

    路由的概念在软件工程中出现,最早是在后端路由中实现的 : 

    • 后端路由阶段
    • 前后端分离阶段
    • 单页面富应用(SPA)

    1. 后端路由阶段

    早期的网站开发整个HTML页面是由服务器来渲染的,服务器直接生产渲染好对应的HTML页面, 返回给客户端进行展示

    多页面服务器如何处理 : 

    • 一个页面有自己对应的网址, 也就是URL
    • URL会发送到服务器, 服务器会通过正则对该URL进行匹配, 并且最后交给一个Controller进行处理
    • Controller进行各种处理, 最终生成HTML或者数据, 返回给前端

    上面的这种操作, 就是后端路由 : 

    • 当我们页面中需要请求不同的路径内容时, 交给服务器来进行处理, 服务器渲染好整个页面, 并且将页面返回给客户端
    • 这种情况下渲染好的页面, 不需要单独加载任何的js和css, 可以直接交给浏览器展示, 这样也有利于SEO的优化

    后端路由的缺点 : 

    • 一种情况是整个页面的模块由后端人员来编写和维护的
    • 另一种情况是前端开发人员如果要开发页面, 需要通过PHP和Java等语言来编写页面代码
    • 而且通常情况下HTML代码和数据以及对应的逻辑会混在一起, 编写和维护都是非常糟糕的事情

    2. 前后端分离阶段

    前端渲染的理解 : 

    • 每次请求涉及到的静态资源都会从静态资源服务器获取,这些资源包括HTML+CSS+JS,然后在前端对这些请求回来的资源进行渲染
    • 需要注意的是,客户端的每一次请求,都会从静态资源服务器请求文件
    • 同时可以看到,和之前的后端路由不同,这时后端只是负责提供API

    前后端分离阶段 : 

    • 随着Ajax的出现, 有了前后端分离的开发模式
    • 后端只提供API来返回数据,前端通过Ajax获取数据,并且可以通过JavaScript将数据渲染到页面中
    • 这样做最大的优点就是前后端责任的清晰,后端专注于数据上,前端专注于交互和可视化上
    • 并且当移动端(iOS/Android)出现后,后端不需要进行任何处理,依然使用之前的一套API即可
    • 目前比较少的网站采用这种模式开发

    3. 单页面富应用(SPA)

    单页面富应用阶段 : 

    • 其实SPA最主要的特点就是在前后端分离的基础上加了一层前端路由
    • 也就是前端来维护一套路由规则
    • 前端路由的核心是  =>  改变URL,但是页面不进行整体的刷新

    二、路由跳转的方式

    1. URL的hash

    • URL的hash也就是锚点(#), 本质上是改变window.location的href属性
    • 可以通过直接赋值location.hash来改变href, 但是页面不发生刷新
    • hash的优势就是兼容性更好,在老版IE中都可以运行,但是缺陷是有一个#,显得不像一个真实的路径

    2. HTML5的History

    history接口是HTML5新增的, 它有六种模式改变URL而不刷新页面

    • replaceState : 替换原来的路径
    • pushState : 使用新的路径
    • popState : 路径的回退
    • go : 向前或向后改变路径
    • forward : 向前改变路径
    • back : 向后改变路径

    3. 区别

    • hash模式带#号比较丑,history模式比较优雅
    • hash兼容IE8以上,history兼容IE10以上
    • hash无需后端配合,history模式需要后端配合保持路径一致,否则刷新可能会404
    • hash的传参是基于url的,如果传递复杂的数据回有体积的限制
    • history模式不仅可以在url里放参数,还可以将数据存放在一个特定的对象中 

    三、Vue-Router基本使用

    1. 概念

    目前前端流行的三大框架, 都有自己的路由实现 : 

    • Angular   =>   ngRouter
    • React   =>   ReactRouter
    • Vue   =>   vue-router

    Vue Router : 

    • Vue.js 的官方路由
    • 让用 Vue.js 构建单页应用(SPA)变得非常容易
    • vue-router是基于路由和组件的
    • 路由用于设定访问路径, 将路径和组件映射起来
    • 在vue-router的单页面应用中, 页面的路径的改变就是组件的切换

    2. 安装

    npm install vue-router

    3. 路由的使用步骤 

    • 第一步:创建路由需要映射的组件(打算显示的页面)
    • 第二步:通过createRouter创建路由对象,并且传入routes和history模式
      • 配置路由映射: 组件和路径映射关系的routes数组
      • 创建基于hash或者history的模式
    • 第三步:使用app注册路由对象(use方法)
    • 第四步:路由使用: 通过

    01 - 创建映射组件

    02 - 创建路由对象 

    在根目录下创建router文件夹,并在其中创建index.js

    1. // 1. 导入创建路由对象 和 创建hash对象
    2. import { createRouter, createWebHashHistory } from 'vue-router';
    3. // 2. 导入组件
    4. import Home from '../views/Home.vue';
    5. import About from '../views/About.vue';
    6. // 3. 配置路由映射关系表
    7. const routes = [
    8. // 路径和组件映射起来
    9. { path: '/home', component: Home },
    10. { path: '/about', component: About }
    11. ];
    12. // 4. 创建路由对象
    13. const router = new createRouter({
    14. // 5. 配置路由跳转模式,这里使用 hash 模式
    15. history: createWebHashHistory(),
    16. // 把映射表放入
    17. routes
    18. });
    19. // 5. 导出
    20. export default router;

    03 - 在 main.js 中导入

    1. import { createApp } from 'vue';
    2. import App from './App.vue';
    3. // 1. 导入路由对象
    4. import router from './router';
    5. // 2. 使用use调用一下
    6. createApp(App).use(router).mount('#app');

    04 - 在App.vue文件中设置

    router-link      =>    跳转链接

    router-view    =>   组件显示的位置

    1. <template>
    2. <div class="app">
    3. App 页面
    4. <br />
    5. <br />
    6. <router-link to="/home">Homerouter-link>
    7. <br />
    8. <router-link to="/about">Aboutrouter-link>
    9. <br />
    10. <br />
    11. <router-view>router-view>
    12. div>
    13. template>

    05 - 效果 

    4. 路由的默认路径 

    以让路径默认跳到到首页

    path配置的是根路径: /      =>       redirect是重定向

    1. const routes = [
    2. // 默认显示的页面 => home
    3. { path: '/', redirect: '/home' },
    4. { path: '/home', component: Home },
    5. { path: '/about', component: About }
    6. ];

    5. 使用history模式 

    1. // 1. 导入创建路由对象
    2. /**
    3. * createWebHashHistory : hash模式
    4. * createWebHistory : history模式
    5. */
    6. import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router';
    7. import Home from '../views/Home.vue';
    8. import About from '../views/About.vue';
    9. const routes = [
    10. { path: '/', redirect: '/home' },
    11. { path: '/home', component: Home },
    12. { path: '/about', component: About }
    13. ];
    14. const router = new createRouter({
    15. // 使用history模式
    16. history: createWebHistory(),
    17. routes
    18. });
    19. // 5. 导出
    20. export default router;

    6. router路由的属性

    • path : 指定跳转的路由地址
    • conponent : 指定路由地址对应的组件
    • name : 路由记录独一无二的名称
      • 可用来跳转
      • 可用来配置动态路由时给某个路由增加子路由
    • meta : 自定义一些数据
    1. const routes = [
    2. {
    3. path: '/',
    4. redirect: '/home'
    5. },
    6. {
    7. name: 'home',
    8. path: '/home',
    9. component: Home,
    10. meth: {
    11. name: 'coderstar',
    12. age: 28
    13. }
    14. },
    15. {
    16. name: 'about',
    17. path: '/about',
    18. component: About
    19. }
    20. ];

    7. notFound

     对于哪些没有匹配到的路由,通常会匹配到固定的某个页面 

    01 - 路由设置

    1. // 如果匹配到任何一个不存在的路径,就显示这个组件
    2. {
    3. name: 'notFound',
    4. path: '/:pathMatch(.*)', // => 地址一样的显示出来 => abc/erfsdf/gsa/e
    5. path: '/:pathMatch(.*)*', // => 如果遇到分级,会解析 / ,变成一个数组 => ['abc','erfsdf','gsa','e']
    6. component: () => import('../views/NotFound.vue')
    7. }

    02 - 组件代码 

    1. <template>
    2. <div>NotFound : 抱歉,出错了,未找到该页面div>
    3. <h2 class="not-found">出错路径为 : {{ $route.params.pathMatch }}h2>
    4. template>
    5. <script setup>
    6. import { useRoute } from 'vue-router';
    7. const route = useRoute();
    8. console.log(route.params.pathMatch);
    9. script>
    10. <style lang="less" scoped>
    11. .not-found {
    12. color: red;
    13. }
    14. style>

    03 - 效果 

    8. router-link 

    router-link   =>   有很多属性可以配置

    01 - to属性

    to : 是一个字符串,或者是一个对象

    指定跳转到哪个组件

    1. <router-link to="/home">Homerouter-link>
    2. <router-link :to="{ path: '/home' }">Homerouter-link>

    02 - replace属性

    不设置的时候

    使用的是 router.push() ,可以点击浏览器的返回,回到上一次记录页面

    设置的时候

    使用的是router.replace(),无法返回到上一次记录页面

    1. <router-link to="/home" replace>Homerouter-link>
    2. <router-link to="/about">Aboutrouter-link>

    03 - active-class属性

    设置激活a元素后应用的class,默认是router-link-active

            默认

            设置

    1. <router-link to="/home" active-class="acttttive">Homerouter-link>
    2. <router-link to="/about">Aboutrouter-link>

    04 - exact-active-class属性

    链接精准激活时,应用于渲染的 的 class,默认是router-link-exact-active

     

    ps : 精准定位路由,用于嵌套路由中

    四、路由懒加载分包处理

    1. 路由懒加载

    当打包构建应用时,JavaScript 包会变得非常大,影响页面加载

    • 如果能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就会更加高效也可以提高首屏的渲染效率 
    • Vue Router默认支持动态来导入组件
    • component可以传入一个组件,也可以接收一个函数,该函数需要放回一个Promise
    • import函数就是返回一个Promise
    1. const routes = [
    2. {
    3. path: '/',
    4. redirect: '/home'
    5. },
    6. {
    7. name: 'home',
    8. path: '/home',
    9. // 路由懒加载
    10. component: () => import('../views/Home.vue'),
    11. meth: {
    12. name: 'coderstar',
    13. age: 28
    14. }
    15. },
    16. {
    17. name: 'about',
    18. path: '/about',
    19. // 路由懒加载
    20. component: () => import('../views/About.vue')
    21. }
    22. ];

    2. 分包处理

    webpack从3.x开始支持对分包进行命名(chunk name)

    1. const routes = [
    2. {
    3. path: '/',
    4. redirect: '/home'
    5. },
    6. {
    7. name: 'home',
    8. path: '/home',
    9. // 路由懒加载
    10. component: () => import('../views/Home.vue'),
    11. meth: {
    12. name: 'coderstar',
    13. age: 28
    14. }
    15. },
    16. {
    17. name: 'about',
    18. path: '/about',
    19. // 路由懒加载,可以顺便设置分包后的名称
    20. component: () => import(/* webpackChunkName: 'about' */ '../views/About.vue')
    21. }
    22. ];

    五、动态路由

    很多时候需要将给定匹配模式的路由映射到同一个组件

    • 例如,有一个 User 组件,它应该对所有用户进行渲染,但是用户的ID是不同的
    • 在Vue Router中,可以在路径中使用一个动态字段来实现,称之为 路径参数

    1. 设置路由

    1. {
    2. name: 'user',
    3. // 动态设定 id 和 name
    4. path: '/user/:id/:name',
    5. component: () => import('../views/User.vue')
    6. }

    2. 跳转设置

    1. <router-link to="/user/111111/coder">User => 1router-link>
    2. <br />
    3. <router-link to="/user/222222/star">User => 2router-link>

    3. 组件中获取值

    1. <template>
    2. <div>我是用户user => {{ $route.params.id }} - {{ $route.params.name }}div>
    3. template>
    4. <script setup>
    5. import { useRoute, onBeforeRouteUpdate } from 'vue-router';
    6. // 2. 在js中获取
    7. // 第一次获取
    8. const route = useRoute();
    9. console.log(route.params.id, route.params.name);
    10. // 切换的时候获取
    11. onBeforeRouteUpdate((to, from) => {
    12. // 当前
    13. console.log('to:', to.params.id, to.params.name);
    14. // 从哪里来
    15. // console.log('from:', from.params.id, from.params.name);
    16. });
    17. script>

    4. 效果 

    六、路由嵌套

    路由嵌套 : 组件的内部可能有多个组件来回切换

    1. 子组件创建

    2. 路由设置

    1. const routes = [
    2. {
    3. path: '/',
    4. redirect: '/home'
    5. },
    6. {
    7. name: 'home',
    8. path: '/home',
    9. component: () => import('../views/Home.vue'),
    10. meth: {
    11. name: 'coderstar',
    12. age: 28
    13. },
    14. // 配置子路由
    15. children: [
    16. {
    17. // 配置重定向,默认进去 home组件 中的 A组件
    18. path: '/home',
    19. redirect: '/home/A'
    20. },
    21. {
    22. // 注 : 这里不需要写全称 => /home/a
    23. path: 'a',
    24. component: () => import('../views/HomeA.vue')
    25. },
    26. {
    27. // 注 : 这里不需要写全称 => /home/b
    28. path: 'b',
    29. component: () => import('../views/HomeB.vue')
    30. }
    31. ]
    32. },
    33. {
    34. name: 'about',
    35. path: '/about',
    36. component: () => import('../views/About.vue')
    37. },
    38. {
    39. name: 'user',
    40. path: '/user/:id/:name',
    41. component: () => import('../views/User.vue')
    42. },
    43. {
    44. name: 'notFound',
    45. path: '/:pathMatch(.*)',
    46. component: () => import('../views/NotFound.vue')
    47. }
    48. ];

    3. 组件设置

    1. <template>
    2. <div>我是home组件div>
    3. <br />
    4. <router-link to="/home/a">toArouter-link>
    5. <br />
    6. <router-link to="/home/b">toBrouter-link>
    7. <br />
    8. <br />
    9. <router-view>router-view>
    10. template>

    4. 效果 

    5. exact-active-class属性

    七、路由的编程式导航,代码的页面跳转

    1. 简单使用

    代码

    1. <button @click="jumpClick('/home')">跳转到home组件button> |
    2. <button @click="jumpClick('/about')">跳转到about组件button>
    3. <script setup>
    4. // 1. 导入
    5. import { useRouter } from 'vue-router';
    6. // 2. 使用
    7. const router = useRouter();
    8. // 3. 监听点击
    9. const jumpClick = (url) => {
    10. // 传入 字符串
    11. router.push(url);
    12. // 传入 对象
    13. // router.push({
    14. // path: url
    15. // });
    16. };
    17. script>

    效果

    2. 跳转方式

    01 - 通过 name 跳转并传递参数

            设置

    1. {
    2. name: 'home', // 用name这个属性来跳转
    3. path: '/home',
    4. component: () => import('../views/Home.vue'),
    5. meth: {
    6. name: 'coderstar',
    7. age: 28
    8. },
    9. children: [
    10. {
    11. path: '/home',
    12. redirect: '/home/A'
    13. },
    14. {
    15. path: 'a',
    16. component: () => import('../views/HomeA.vue')
    17. },
    18. {
    19. path: 'b',
    20. component: () => import('../views/HomeB.vue')
    21. }
    22. ]
    23. },
    24. {
    25. name: 'about', // 用name这个属性来跳转
    26. path: '/about',
    27. component: () => import('../views/About.vue')
    28. }

            传递

    1. <button @click="jumpClick('home')">跳转到home组件button> |
    2. <button @click="jumpClick('about')">跳转到about组件button>
    3. <script setup>
    4. // 1. 导入
    5. import { useRouter } from 'vue-router';
    6. // 2. 使用
    7. const router = useRouter();
    8. const jumpClick = (name) => {
    9. // 3. name跳转
    10. router.push({
    11. // name
    12. name: name,
    13. // 传递参数
    14. query: {
    15. message: '我是迪迦'
    16. }
    17. });
    18. };
    19. script>

            获取

    1. <template>
    2. <div>我是about组件div>
    3. {{ $route.query }}
    4. template>
    5. <script setup>
    6. import { onMounted } from 'vue';
    7. import { useRoute } from 'vue-router';
    8. const route = useRoute();
    9. onMounted(() => {
    10. // 2. js中获取
    11. console.log('获取到的参数', route.query);
    12. });
    13. script>

            效果

            缺点

    子路由的重定向路由会失效,看看上方home组件的即可知道

    02 - 通过 path 跳转并传递参数

            设置

    1. {
    2. name: 'home',
    3. path: '/home', // 用path这个属性来跳转
    4. component: () => import('../views/Home.vue'),
    5. meth: {
    6. name: 'coderstar',
    7. age: 28
    8. },
    9. children: [
    10. {
    11. path: '/home',
    12. redirect: '/home/A'
    13. },
    14. {
    15. path: 'a',
    16. component: () => import('../views/HomeA.vue')
    17. },
    18. {
    19. path: 'b',
    20. component: () => import('../views/HomeB.vue')
    21. }
    22. ]
    23. },
    24. {
    25. name: 'about',
    26. path: '/about', // 用path这个属性来跳转
    27. component: () => import('../views/About.vue')
    28. },

            传递

    1. <button @click="jumpClick('/home')">跳转到home组件button> |
    2. <button @click="jumpClick('/about')">跳转到about组件button>
    3. <script setup>
    4. // 1. 导入
    5. import { useRouter } from 'vue-router';
    6. // 2. 使用
    7. const router = useRouter();
    8. const jumpClick = (url) => {
    9. // 3. path跳转
    10. router.push({
    11. // name
    12. path: url,
    13. // 传递参数
    14. query: {
    15. message: '迪迦变身!!!'
    16. }
    17. });
    18. };
    19. script>

            获取

    1. <template>
    2. <div>我是about组件div>
    3. {{ $route.query }}
    4. template>
    5. <script setup>
    6. import { onMounted } from 'vue';
    7. import { useRoute } from 'vue-router';
    8. const route = useRoute();
    9. onMounted(() => {
    10. // 2. js中获取
    11. console.log('获取到的参数', route.query);
    12. });
    13. script>

            效果

    3. 前进后退

    back

    back  =>  想后回退

    1. <template>
    2. <div class="about">
    3. <h2>About: {{ $route.query }}h2>
    4. <button @click="backBtnClick">返回button>
    5. div>
    6. template>
    7. <script setup>
    8. import { useRouter } from 'vue-router'
    9. const router = useRouter()
    10. function backBtnClick() {
    11. router.back()
    12. }
    13. script>

    forward

    forward  =>  想前进步

    1. <template>
    2. <div class="about">
    3. <h2>About: {{ $route.query }}h2>
    4. <button @click="backBtnClick">返回button>
    5. div>
    6. template>
    7. <script setup>
    8. import { useRouter } from 'vue-router'
    9. const router = useRouter()
    10. function backBtnClick() {
    11. router.forward()
    12. }

    go

    go  =>  可前进,可后退

    1. <template>
    2. <div class="about">
    3. <h2>About: {{ $route.query }}h2>
    4. <button @click="backBtnClick">返回button>
    5. div>
    6. template>
    7. <script setup>
    8. import { useRouter } from 'vue-router'
    9. const router = useRouter()
    10. function backBtnClick() {
    11. // go(1) -> forward()
    12. // go(-1) -> back()
    13. router.go(200)
    14. }
    15. script>

    4. push | replace

    01 - push

    1. <script setup>
    2. import { useRouter } from 'vue-router';
    3. const router = useRouter();
    4. const jumpClick = (url) => {
    5. // 跳转可以回溯
    6. router.push(url);
    7. };
    8. script>

    02 - replace

    1. <script setup>
    2. import { useRouter } from 'vue-router';
    3. const router = useRouter();
    4. const jumpClick = (url) => {
    5. // 跳转不可回溯,没有记录
    6. router.replace(url)
    7. };
    8. script>

    八、动态管理路由对象

    某些情况下可能需要动态的来添加路由

    根据用户不同的权限,注册不同的路由

    1. 添加路由

    01 - 添加顶层路由

    1. // 添加顶层路由
    2. let isAdmin = true;
    3. if (isAdmin) {
    4. router.addRoute({
    5. name: 'admin',
    6. path: '/admin',
    7. component: () => import('../views/Admin.vue')
    8. });
    9. }
    10. // 5. 导出
    11. export default router;

    02 - 添加次级路由

    1. // 添加次级路由
    2. let isAdmin = true;
    3. if (isAdmin) {
    4. // 第一个参数传 父级的name
    5. router.addRoute('home', {
    6. name: 'home-a1',
    7. path: 'a1', // 实际上是 /home/a1
    8. component: () => import('../views/HomeA1.vue')
    9. });
    10. }
    11. // 5. 导出
    12. export default router;

    03 - 添加三级路由

    1. let isAdmin = true;
    2. if (isAdmin) {
    3. router.addRoute('home', {
    4. name: 'home-a1',
    5. path: 'a1',
    6. component: () => import('../views/HomeA1.vue')
    7. });
    8. // 三级路由,需要在 home-a1 中加上 router-view 哦
    9. router.addRoute('home-a1', {
    10. name: 'home-a2',
    11. path: 'a2', // 实际上是 /home/a1/a2
    12. component: () => import('../views/HomeA2.vue')
    13. });
    14. }
    15. // 5. 导出
    16. export default router;

    2. 删除路由

    01 - 方式一

    添加一个name相同的路由

    新添加的路由,会把之前同名的路由给覆盖掉,因为name属性是唯一的

    1. router.addRoute({
    2. name: 'home-a1',
    3. path: 'a1', // '/a1'
    4. component: () => import('../views/HomeA1.vue')
    5. });
    6. // 会覆盖掉上面的
    7. router.addRoute({
    8. name: 'home-a1',
    9. path: 'a999999', // '/a999999'
    10. component: () => import('../views/HomeA2.vue')
    11. });

    02 - 方式二

    通过removeRoute方法,传入路由的名称

    1. router.addRoute({
    2. name: 'home-a1',
    3. path: '/a1',
    4. component: () => import('../views/HomeA1.vue')
    5. });
    6. // 删除路由
    7. router.removeRoute('home-a1');

    03 - 方式三

    通过addRoute方法的返回值回调

    1. // 1. 增加路由后,会有一个返回值方法
    2. const removeA1 = router.addRoute({
    3. name: 'home-a1',
    4. path: '/a1',
    5. component: () => import('../views/HomeA1.vue')
    6. });
    7. // 2. 调用该返回值方法,即可删除路由
    8. removeA1();

    3. 检查路由

     router.hasRoute()  =>  检查路由是否存在

    1. console.log(router.hasRoute('about')); // true
    2. router.removeRoute('about');
    3. console.log(router.hasRoute('about')); // false

    4. 获取所有路由

     router.getRoutes():获取一个包含所有路由记录的数组

    console.log(router.getRoutes()); // 一个路由数组

    九、路由导航守卫钩子

    vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航

    1. 全局的前置守卫 

    全局的前置守卫 beforeEach 是在导航触发时会被回调的

    进行任何的路由跳转之前, 传入的beforeEach中的函数都会被回调

    01 - 参数

    • to : 即将进入的路由Route对象
    • from : 即将离开的路由Route对象
    • next ( 可选 ) : 不推荐使用
      • Vue2中是通过next函数来决定如何进行跳转的
      • Vue3中是通过返回值来控制的,不再推荐使用next函数,这是因为开发中很容易调用多次next

    02 - 返回值 

    • false : 取消当前导航  =>  不跳转
    • 不返回或者undefined : 进行默认导航  =>  该去哪去哪
    • 返回一个路由地址  =>  指定跳去哪
      • 可以是一个string类型的路径
      • 可以是一个对象,对象中包含path、query、params等信息

    03 - 登录守卫功能栗子 🌰

            router.js代码

    1. // 路由导航首位
    2. /**
    3. * to : 去哪个路由
    4. * from : 从哪里来
    5. */
    6. // 1. 设定需要登录才能进的页面
    7. const needLoginRoutePath = ['/about', '/user/222222/star'];
    8. router.beforeEach((to, from) => {
    9. // 2. 判断是否进入需要登录后才能进的页面
    10. if (needLoginRoutePath.includes(to.path)) {
    11. // 3. 获取登录状态
    12. const token = sessionStorage.getItem('token');
    13. // 4. 判断 => 如果没登录,跳转到登录页面
    14. if (!token) {
    15. // 01. 返回登录页面的路径即可
    16. // return '/login';
    17. // 02. 同时传递数据过去
    18. return {
    19. path: '/login',
    20. query: {
    21. toPath: to.path
    22. }
    23. };
    24. }
    25. }
    26. });
    27. // 5. 导出
    28. export default router;

            login组件代码

    1. <template>
    2. <div>
    3. Login页面
    4. <br />
    5. 账号 : <input type="text" />
    6. <br />
    7. 密码 : <input type="password" />
    8. <br />
    9. <button @click="loginClick">登录button>
    10. div>
    11. template>
    12. <script setup>
    13. import { useRoute, useRouter } from 'vue-router';
    14. const route = useRoute();
    15. const router = useRouter();
    16. // 1. 监听登录按钮点击
    17. const loginClick = () => {
    18. // 2. 发送请求,进行登录
    19. // balabalbala~
    20. // 3. 存储token到sessionStorage
    21. sessionStorage.setItem('token', '冲啊,迪迦!!!!!!');
    22. console.log('我要去的页面', route.query.toPath);
    23. // 4. 跳转到指定页面
    24. router.push(route.query.toPath);
    25. };
    26. script>

            效果

    2. 其他的导航守卫 导航守卫 | Vue Router

    3. 完整的导航解析流程

     

  • 相关阅读:
    深入理解final关键字
    【数据结构】树与二叉树(七):二叉树的遍历
    Hashtable 相关面试集合(2022)
    JVM成神之路(十一) -- JVM常用命令解析
    第80步 时间序列建模实战:GRNN回归建模
    Jackson
    大模型参数高效微调技术原理综述(二)-BitFit、Prefix Tuning、Prompt Tuning
    微信小程序云开发教程——墨刀原型工具入门(添加交互事件)
    安利2款好用的笔记软件给你们
    JAVA基础 - Serializable的作用与用法
  • 原文地址:https://blog.csdn.net/a15297701931/article/details/127237838