当前位置: 首页 > news >正文

【Vue3】嵌套路由

【Vue3】嵌套路由

  • 背景
  • 简介
  • 开发环境
  • 开发步骤及源码

背景

随着年龄的增长,很多曾经烂熟于心的技术原理已被岁月摩擦得愈发模糊起来,技术出身的人总是很难放下一些执念,遂将这些知识整理成文,以纪念曾经努力学习奋斗的日子。本文内容并非完全原创,大多是参考其他文章资料整理所得,感谢每位技术人的开源精神。

简介

本文介绍 Vue3 中嵌套路由的基本写法。

开发环境

分类名称版本
操作系统WindowsWindows 11
IDEVisual Studio Code1.91.1

开发步骤及源码

1> 在 【Vue3】路由基础 的基础上新增 3 个页面组件。

  • Permission.vue

    <template><div class="permission">这是权限页面</div>
    </template><script setup lang="ts">
    </script><style scoped lang="scss">
    </style>
    
  • Log.vue

    <template><div class="log">这是日志页面</div>
    </template><script setup lang="ts">
    </script><style scoped lang="scss">
    </style>
    
  • Warn.vue

    <template><div class="warn">这是告警页面</div>
    </template><script setup lang="ts">
    </script><style scoped lang="scss">
    </style>
    

2> 修改 src/router/index.ts,添加新增的 3 个页面组件的路由配置,全部置于 /system 下作为二级路由使用。

import { createRouter, createWebHistory } from 'vue-router'
import Dashboard from '@/pages/Dashboard.vue'
import Log from '@/pages/Log.vue'
import Permission from '@/pages/Permission.vue'
import Warn from '@/pages/Warn.vue'
import System from '@/pages/System.vue'
import About from '@/pages/About.vue'const router = createRouter({// 路由器工作模式history: createWebHistory(),routes: [{path: '/dashboard',component: Dashboard},{path: '/system',component: System,children: [{path: 'permission',component: Permission},{path: 'log',component: Log},{path: 'warn',component: Warn}]},{path: '/about',component: About}]
})export default router

3> 修改 System.vue 页面组件,添加导航到 3 个新增页面组件的路由功能。

<template><div class="system"><div class="navigate"><RouterLink to="/system/permission" class="link" active-class="link-active">权限</RouterLink><RouterLink to="/system/log" class="link" active-class="link-active">日志</RouterLink><RouterLink to="/system/warn" class="link" active-class="link-active">告警</RouterLink></div><hr><div class="content"><RouterView /></div></div>
</template><script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router'
</script><style scoped lang="scss">
.system {padding: 20px 10px;.navigate {margin-bottom: 20px;.link {background: #eee;border-radius: 3px;color: #aaa;margin-right: 5px;padding: 5px 15px;text-decoration: none;}.link-active {background: #75C5BA;color: blue;}}
}
</style>

4> 执行命令 npm run dev 启动应用,浏览器访问:http://localhost:5173/,点击左侧菜单进入 系统管理 页面,点击顶部按钮观察路由切换效果。
在这里插入图片描述

http://www.lryc.cn/news/424824.html

相关文章:

  • pygame小游戏
  • .Net Core IIS 程序报错 Access to the path c:\\windows\\TEMP\\poifiles is denied
  • 交换机VLAN配置中Tagged与Untagged端口的差异和应用区别
  • @OneToOne注解的作用
  • vue动画、过渡效果
  • 在 Vue 3 项目中使用 Element UI Plus <el-calendar>组件与时区处理
  • 【系统架构设计】计算机网络
  • 《中国数据库前世今生》——历史的深度与未来的展望
  • web前端之实现霓虹灯背景魔术卡、旋转的背景动画、模糊效果、边框、变量、filter
  • 几款免费的时序数据库对比
  • 基于springboot的乐享田园系统
  • 深入解析Objective-C中NSParagraphStyle的段落样式处理艺术
  • Qt编程技巧小知识点(2)GPIB缓存区数据读取
  • 数的个位相加
  • 专业技能(挖坑填坑)——MYSQL的索引、日志、事务、存储引擎、锁机制等相关原理
  • C++(27): 线程池
  • 每日一题-贪心算法
  • PSO 算法实例(手动推导过程)
  • 解决antd TreeSelect 返回值不包含父节点问题 -自定义组件(react)
  • 花四小时,写了个在线实时绘制等值面图小软件,明晚上线,喜欢的小伙伴关注哦
  • c++的vector用法
  • 【Linux网络】Linux网络初探:开启网络世界的大门
  • 目录、用户与组、出错相关函数、时间函数
  • <keep-alive> 一分钟了解
  • Android 启动动画太生硬
  • 深度学习中常用概念总结
  • 进 程
  • Taro-UI
  • TypeScript 之 JavaScript文件类型检查
  • 基本数据类型变量间的自动提升与强制转换以及进制的转换