Vue3从入门到实战:路由知识点

淩亂°似流年 2024-04-26 01:45 190阅读 0赞

本人在B站上关于vue3的尚硅谷的课程,以下是整理一些笔记。

1.两个知识点

1.路由组件通常存放在pagesviews文件夹,一般组件通常存放在components文件夹。

组件可以分为:

1. 一般组件:亲手写标签出来的

2. 路由组件:靠路由的规则渲染出来的

比如:

  1. routes:[//一个个的路由规则
  2. {
  3. path:'/home',
  4. component:Home
  5. },
  6. {
  7. path:'/about',
  8. component:About
  9. },
  10. {
  11. path:'/news',
  12. component:News
  13. },
  14. ]

遵循vue中的规则:

一般组件放在components文件夹中,路由组件放在pagesviews文件夹

841cbdab417342b6af2579ff26d2edaa.png


2.通过点击导航,视觉效果上“消失” 了的路由组件,默认是被卸载掉的,需要的时候再去挂载。

比如我在路由组件About中使用生命周期钩子来输出日志信息来观察展示区是如何”消失的”

  1. <template>
  2. <div class="about">
  3. <h2>大家好,欢迎来到小李同学的博客</h2>
  4. </div>
  5. </template>
  6. <script setup lang="ts" name="About">
  7. import {onMounted,onUnmounted} from 'vue'
  8. onMounted(()=>{
  9. console.log('About组件挂载了')
  10. })
  11. onUnmounted(()=>{
  12. console.log('About组件卸载了')
  13. })
  14. </script>
  15. <style scoped>
  16. .about {
  17. display: flex;
  18. justify-content: center;
  19. align-items: center;
  20. height: 100%;
  21. color: rgb(85, 84, 84);
  22. font-size: 18px;
  23. }
  24. </style>

1324a27529bf410a97e6966d1346472d.png

76c94fc23def440ebc18f84660a73039.png

2d739b255e234c77ba180e7b24ec5786.png

2.路由器工作模式

1.History模式:就像你在浏览器中打开一个新的网页一样。当你在应用程序中切换页面时,URL会更新,但页面不会重新加载。这样的URL看起来更加友好和自然,就像传统的网页链接一样。

比如:我现在写的博客的网址也是

d2d308257d3c48b980aa3b29ea1b4463.png

优点:URL更加美观,不带有#,更接近传统的网站URL

缺点:后期项目上线,需要服务端配合处理路径问题,否则刷新会有404错误。

2.Hash模式:就像你在一个长网页中滚动到不同的章节。当你在应用程序中切换页面时,URL会在#符号后面加上一段标识,但浏览器不会发送请求到服务器。这样的URL看起来有点奇怪,但它对于一些特殊的环境(比如旧版浏览器)是有效的。

比如:这段代码的使用

  1. //创建一个路由器并暴露出去
  2. //第一步:引入creatRouter
  3. import{createRouter,createWebHistory,createWebHashHistory} from'vue-router'
  4. //引入一个一个可能要呈现的组件
  5. import Home from '@/pages/Home.vue'
  6. import About from '@/pages/About.vue'
  7. import News from '@/pages/News.vue'
  8. //第二步:创建路由器
  9. const router =createRouter({
  10. history:createWebHashHistory(),//路由器的Hash工作模式
  11. routes:[//一个个的路由规则
  12. {
  13. path:'/home',
  14. component:Home
  15. },
  16. {
  17. path:'/about',
  18. component:About
  19. },
  20. {
  21. path:'/news',
  22. component:News
  23. },
  24. ]
  25. })
  26. export default router//定义好后暴露出去router

e092d163f86d44519cf6e38fe83abbdc.png

优点:兼容性更好,因为不需要服务器端处理路径。

缺点:URL带有#不太美观,且在SEO优化方面相对较差。

两者的应用场景:

如果你的应用程序主要在现代浏览器中运行,并且你希望URL看起来更加友好和自然,那么可以选择使用history模式。如果你需要在旧版浏览器中兼容,或者你的应用程序是一个单页应用(SPA),可以选择使用hash模式。


3.to的两者写法

在用于生成导航链接的标签<router-link>组件中to**的两种常见使用方式,分别是字符串写法和对象写法。**

  1. <!-- App.vue 有三种标签,html(结构标签) ,script(交互标签) ,style(样式,用于好看) -->
  2. <template>
  3. <div class = 'app'>
  4. <Header/>
  5. <!-- 导航区 -->
  6. <div class = 'navigate'>
  7. <RouterLink to = '/home'active-class="active" >首页</RouterLink>
  8. <RouterLink to = '/news'active-class="active" >新闻</RouterLink>
  9. <RouterLink to = '/about'active-class="active " >关于</RouterLink>
  10. </div>
  11. <!-- 展示区 -->
  12. <div class = 'main-content'>
  13. <RouterView></RouterView>
  14. </div>
  15. </div>
  16. </template>
  17. <script lang="ts" setup name = "App">
  18. import { RouterView,RouterLink} from 'vue-router';
  19. import Header from './components/Header.vue'
  20. </script>
  21. <style>
  22. /* App */
  23. .navigate {
  24. display: flex;
  25. justify-content: space-around;
  26. margin: 0 100px;
  27. }
  28. .navigate a {
  29. display: block;
  30. text-align: center;
  31. width: 90px;
  32. height: 40px;
  33. line-height: 40px;
  34. border-radius: 10px;
  35. background-color: gray;
  36. text-decoration: none;
  37. color: white;
  38. font-size: 18px;
  39. letter-spacing: 5px;
  40. }
  41. .navigate a.active {
  42. background-color: #64967E;
  43. color: #ffc268;
  44. font-weight: 900;
  45. text-shadow: 0 0 1px black;
  46. font-family: 微软雅黑;
  47. }
  48. .main-content {
  49. margin: 0 auto;
  50. margin-top: 30px;
  51. border-radius: 10px;
  52. width: 90%;
  53. height: 400px;
  54. border: 1px solid;
  55. }
  56. </style>

第一种:to的字符串写法

23bda5b0633a4b71b65504353d803768.png

第二种:to的对象写法

d9f3d017447e4677bc1c1194b5607687.png

两者的区别:

使用字符串写法时,你可以直接将目标路由的路径作为字符串传递给to属性,例如to="/home"

使用对象写法时,你可以通过一个对象来指定目标路由的各种属性,例如{ path: '/home' }。除了path属性,你还可以在对象中指定其他路由属性,如nameparamsquery等,以满足更复杂的路由导航需求。

4.命名路由

作用:可以简化路由跳转及传参。

  1. routes:[
  2. {
  3. name:'zhuye',
  4. path:'/home',
  5. component:Home
  6. },
  7. {
  8. name:'xinwen',
  9. path:'/news',
  10. component:News,
  11. },
  12. {
  13. name:'guanyu',
  14. path:'/about',
  15. component:About
  16. }

跳转路由:

  1. <!--简化前:需要写完整的路径(to的字符串写法) -->
  2. <router-link to="/news/detail">跳转</router-link>
  3. <!--简化后:直接通过名字跳转(to的对象写法配合name属性) -->
  4. <router-link :to="{name:'guanyu'}">跳转</router-link>

5.嵌套路由

我们在关于新闻的组件中可以再嵌套一个子路由,用来展示不同新闻的不同详情。

此时,新闻的标题成为导航区,点击后详情出现展示区内。

02825681f5ad4ac38c1d20cd45c8dde4.png

下面是嵌套路由的步骤:

1.编写News的子路由:Detail.vue组件

3842f3c23e514762a11132a174e9f4a0.png

代码如下:

  1. <template>
  2. <ul class="news-list">
  3. <li>编号:xxx</li>
  4. <li>标题:xxx</li>
  5. <li>内容:xxx</li>
  6. </ul>
  7. </template>
  8. <script setup lang="ts" name="About">
  9. </script>
  10. <style scoped>
  11. .news-list {
  12. list-style: none;
  13. padding-left: 20px;
  14. }
  15. .news-list>li {
  16. line-height: 30px;
  17. }
  18. </style>

2.在index.ts文件配置路由规则,使用children配置项:

78ed074b09514e56ab81dcd0c3c088c6.png

代码如下:

  1. // 创建一个路由器,并暴露出去
  2. // 第一步:引入createRouter
  3. import {createRouter,createWebHistory,createWebHashHistory} from 'vue-router'
  4. // 引入一个一个可能要呈现组件
  5. import Home from '@/pages/Home.vue'
  6. import News from '@/pages/News.vue'
  7. import About from '@/pages/About.vue'
  8. import Detail from '@/pages/Detail.vue'
  9. // 第二步:创建路由器
  10. const router = createRouter({
  11. history:createWebHistory(), //路由器的工作模式(稍后讲解)
  12. routes:[ //一个一个的路由规则
  13. {
  14. name:'zhuye',
  15. path:'/home',
  16. component:Home
  17. },
  18. {
  19. name:'xinwen',
  20. path:'/news',
  21. component:News,
  22. children:[
  23. {
  24. path:'detail',
  25. component:Detail
  26. }
  27. ]
  28. },
  29. {
  30. name:'guanyu',
  31. path:'/about',
  32. component:About
  33. },
  34. ]
  35. })
  36. // 暴露出去router
  37. export default router

3.在关于新闻的”News”组件添加子路

注意:

在新闻的导航区里面,再写入展示区div

75c40b7b379e47cda68edb1ee6eab426.png

代码如下:

  1. <template>
  2. <div class="news">
  3. <!-- 导航区 -->
  4. <ul>
  5. <li v-for="news in newsList" :key="news.id">
  6. <RouterLink to="/news/detail">{
  7. {news.title}}</RouterLink>
  8. </li>
  9. </ul>
  10. <!-- 展示区 -->
  11. <div class="news-content">
  12. <RouterView></RouterView>
  13. </div>
  14. </div>
  15. </template>
  16. <script setup lang="ts" name="News">
  17. import {reactive} from 'vue'
  18. import {RouterView,RouterLink} from 'vue-router'
  19. const newsList = reactive([
  20. {id:'title01',title:'很好的抗癌食物',content:'西篮花'},
  21. {id:'title02',title:'如何一夜暴富',content:'学IT'},
  22. {id:'title03',title:'震惊,万万没想到',content:'明天是周一'},
  23. {id:'title04',title:'好消息!好消息!',content:'快过年了'}
  24. ])
  25. </script>
  26. <style scoped>
  27. /* 新闻 */
  28. .news {
  29. padding: 0 20px;
  30. display: flex;
  31. justify-content: space-between;
  32. height: 100%;
  33. }
  34. .news ul {
  35. margin-top: 30px;
  36. list-style: none;
  37. padding-left: 10px;
  38. }
  39. .news li>a {
  40. font-size: 18px;
  41. line-height: 40px;
  42. text-decoration: none;
  43. color: #64967E;
  44. text-shadow: 0 0 1px rgb(0, 84, 0);
  45. }
  46. .news-content {
  47. width: 70%;
  48. height: 90%;
  49. border: 1px solid;
  50. margin-top: 20px;
  51. border-radius: 10px;
  52. }
  53. </style>

展示:

399d68d3a920456b97d2651dd886bcae.png

发表评论

表情:
评论列表 (有 0 条评论,190人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue2+3入门实战

    作为IT技术相关行业不可或缺的岗位之一,前端开发工程师就业前途广阔,一直是很多同学心中转行的首选行业。但很多人还没开始,便被一系列问题难倒了,比如:前端该如何入门?路线图

    相关 VUE3 Router

    > 使用Vue Router可以在不同的组件间切换,根据不同的url地址,路由跳转到不同的组件 > > 提供了俩个路由组件 > > <router-link>:用来定义导航