聊城做网站推广地方,WordPress注册插件中文,网站建设孝感,福州网站建设名列前茅【Vue3】路由基础 背景简介开发环境开发步骤及源码总结 背景
随着年龄的增长#xff0c;很多曾经烂熟于心的技术原理已被岁月摩擦得愈发模糊起来#xff0c;技术出身的人总是很难放下一些执念#xff0c;遂将这些知识整理成文#xff0c;以纪念曾经努力学习奋斗的日子。本… 【Vue3】路由基础 背景简介开发环境开发步骤及源码总结 背景
随着年龄的增长很多曾经烂熟于心的技术原理已被岁月摩擦得愈发模糊起来技术出身的人总是很难放下一些执念遂将这些知识整理成文以纪念曾经努力学习奋斗的日子。本文内容并非完全原创大多是参考其他文章资料整理所得感谢每位技术人的开源精神。
简介
本文介绍 Vue3 中路由的基本写法。
开发环境
分类名称版本操作系统WindowsWindows 11IDEVisual Studio Code1.91.1
开发步骤及源码
1 创建 Vue3 工程参考【Vue3】工程创建及目录说明。
2 删除 src 目录下 assets 和 components 目录。
3 修改 src 目录下 main.ts。
import { createApp } from vue
import App from ./App.vuecreateApp(App).mount(#app)4 创建三个页面组件注意与功能组件不同不放在 src/components 目录下页面组件一般放在 pages 或 views 目录下。 Dashboard.vue templatediv classdashboard这是仪表盘页面/div
/templatescript setup langts
/scriptstyle scoped langscss
/styleSystem.vue templatediv classsystem这是系统管理页面/div
/templatescript setup langts
/scriptstyle scoped langscss
/styleAbout.vue templatediv classabout这是关于页面/div
/templatescript setup langts
/scriptstyle scoped langscss
/style5 执行 npm i vue-router 命令安装路由组件。
6 在 src 下创建 router 目录并在其中创建 index.ts 文件此文件作用是创建并暴露路由器。
import { createRouter, createWebHistory } from vue-router
import Dashboard from /pages/Dashboard.vue
import System from /pages/System.vue
import About from /pages/About.vueconst router createRouter({// 路由器工作模式history: createWebHistory(),routes: [{path: /dashboard,component: Dashboard},{path: /system,component: System},{path: /about,component: About}]
})export default router7 修改 main.ts 引入并使用路由器。
import { createApp } from vue
import App from ./App.vue
import router from ./routercreateApp(App).use(router).mount(#app)8 修改根组件 App.vue调用路由器实现跳转功能。
templateh1 classtitleVue3路由/h1hrdiv classroutediv classmenudiv classmenu-itemRouterLink to/dashboard active-classactive仪表盘/RouterLink/divdiv classmenu-itemRouterLink to/system active-classactive系统管理/RouterLink/divdiv classmenu-itemRouterLink to/about active-classactive关于/RouterLink/div/divdiv classcontentRouterView //div/div
/templatescript setup langts
import { RouterLink, RouterView } from vue-router
/scriptstyle scoped langscss
.title {text-align: center;
}
.route {display: flex;justify-content: center;.menu {width: 200px;height: 500px;background-color: #F1F2F3;border-radius: 6px;.menu-item {height: 40px;line-height: 40px;text-align: center;margin: 5px;border-radius: 3px;text-decoration: none;}.menu-item:hover {background-color: white;cursor: pointer}a {text-decoration: none;}.active {color: #00AEEC;}}.content {width: 600px;height: 500px;margin-left: 10px;border: 1px solid #F1F2F3;}
}
/style9 执行命令 npm run dev 启动应用浏览器访问http://localhost:5173/点击左侧菜单观察页面变化。
总结
关键步骤
安装路由组件npm i vue-router创建并暴露路由器src/router/index.ts包括 引入 createRouter 用于创建路由器通过 createRouter 参数对象的 history 属性配置路由器工作模式路由器工作模式有两类createWebHistory() 和 createWebHashHistory()本文使用的是 createWebHistory()两类工作模式间的差异将在其他文章中说明通过 createRouter 参数对象的 routes 属性配置路由每个路由由一个路径 path 和一个组件 component 构成暴露路由 export default router。 引入路由器src/main.ts作用是 全局注册 RouterLink 和 RouterView 组件添加全局 $router 和 $route 属性启用 useRouter() 和 useRoute() 组合式函数触发路由器解析初始路由。 使用 Vue Router 提供的组件实现路由功能 RouterLink代替常规的 a 标签创建链接使得能够在不重新加载页面的情况下改变 URL处理 URL 的生成、编码和其他功能RouterView渲染当前 URL 路径对应的页面组件。