Vue3从入门到实战:路由的query和params参数

r囧r小猫 2024-04-26 01:47 195阅读 0赞

在Vue 3中,我们可以通过路由的查询参数来传递数据。这意味着我们可以在不同的页面之间传递一些信息,以便页面可以根据这些信息来显示不同的内容或执行不同的操作。

查询参数的使用方式类似于在URL中添加附加信息,以便页面之间可以根据这些信息进行交互和通信。这在很多应用中都非常有用,例如搜索功能、过滤功能、分页功能等等。

举个例子,假设我们有一个商品列表页面,用户可以在搜索框中输入关键字来搜索商品。当用户点击搜索按钮时,我们可以将输入的关键字作为查询参数添加到URL中,然后跳转到商品列表页面。在商品列表页面,我们可以通过读取查询参数的值来获取用户输入的关键字,并根据关键字来展示匹配的商品。

比如我在News组件中的detail使用了<RouterLink>组件来创建一个链接,指向径/news/detail 并且附带了查询参数a=哇哈哈,b=华为,c=小米{、显示了新闻的标题。当用户点击这个链接时,URL将会变成/news/detail?a=1,b=2,c=3。注意,查询参数使用问号(?)来分隔路径和查询字符串。

未加参数前:

f31dde7129bc48919e245fe517cc2e20.png

加参数后:

62a93f119cca414fbd21fd5d3c6632f7.png

当我点击新闻里的标题时,就会看到路径中附带的参数

ca46183fcc9f403cba610420e2b10a02.png

但这并不是动态参数的绑定,即无论你点击哪个新闻标题,都是出现同一样的URL。

42186ad3fd944476a3772464ad3cbf45.png

所以现在要讲到动态参数的绑定,即我点击不同的新闻标题时,可以对应出现不同的参数,

1.路由-query参数

路由的查询参数是一种在URL中传递数据的机制。它们可以用于在不同的路由之间传递参数,以便组件可以根据这些参数进行不同的行为或显示不同的内容。

1.1 query参数的第一种写法

1.News组件传递query参数。
注意:

  • 在to前面加上”:”
  • 在to的” “内加入反引号`(数字1的左边)
  • 用$ 连接对象

4d5be7524c7548ea8c1a8ed79916d58e.png

2.query传参后在detail组件中修改内容

2840bf06cf3d488d82991eb307e3e14c.png

解析:

以上使用了route.query来访问查询参数。通过route.query.idroute.query.titleroute.query.content,可以获取URL中的idtitlecontent查询参数的值,并将它们显示在列表项中。

<script setup>部分,使用useRoute()函数从Vue Router中导入了route对象,并将它设置为响应式变量。这样就可以在模板中使用route.query来访问查询参数的值。
3.展示

87aed24a69fc46f38d8ac1e299a9dbb3.png

e3f87ac599604aadb3928c474e2cce77.png

50a832da1b1f4f08aab174402533b871.png

77fe1130ef544c8eb88832be0fba9ab3.png

News组件代码:

  1. <template>
  2. <div class="news">
  3. <!-- 导航区 -->
  4. <ul>
  5. <li v-for="news in newsList" :key="news.id">
  6. <RouterLink :to="`/news/detail?id=${news.id}&title=${news.title}&content=${news.content}`">{
  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>

Detail组件代码:

  1. <template>
  2. <ul class="news-list">
  3. <li>编号:{
  4. { route.query.id }}</li>
  5. <li>标题:{
  6. { route.query.title }}</li>
  7. <li>内容:{
  8. { route.query.content }}</li>
  9. </ul>
  10. </template>
  11. <script setup lang="ts" name="About">
  12. import { useRoute } from 'vue-router';
  13. let route = useRoute();
  14. </script>
  15. <style scoped>
  16. .news-list {
  17. list-style: none;
  18. padding-left: 20px;
  19. }
  20. .news-list>li {
  21. line-height: 30px;
  22. }
  23. </style>

1.2 query参数的第二种写法

跳转并携带query参数(to的对象写法)

6e9e7e61ad714a87aa894f31d6b8bf55.png

代码:

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

补充:

0b7a90f5dd7744a18fdb138e3da85c44.png

有时候,你会觉得比较冗余,是否能简化一下,可以的。

be112c737b88494096c6e253ddde499b.png 整体代码:

  1. <template>
  2. <ul class="news-list">
  3. <li>编号:{
  4. { query.id }}</li>
  5. <li>标题:{
  6. { query.title }}</li>
  7. <li>内容:{
  8. { query.content }}</li>
  9. </ul>
  10. </template>
  11. <script setup lang="ts" name="About">
  12. import {toRefs} from 'vue'
  13. import {useRoute} from 'vue-router'
  14. let route = useRoute()
  15. let {query} = toRefs(route)
  16. </script>
  17. <style scoped>
  18. .news-list {
  19. list-style: none;
  20. padding-left: 20px;
  21. }
  22. .news-list>li {
  23. line-height: 30px;
  24. }
  25. </style>

2.路由-params参数

2.1 params参数的第一种写法

1.还原Detail组件

18351e6850894e1ea88a14d179d55afa.png

2.还原News组件

837845dfff504785ac3e810bf0c0d97d.png 3.在index.ts文件中子路的规则下占位

f66cdb9fdd50413899b424a69905faa3.png

4.返回News组件中传入参数

450afe612d814939b33d504c95bda567.png

5.我们可以通过console.log(route)观察params的参数(这步骤可无)

10a28af7b249483f883f7c3a93594f41.png

8c5e0e7f7c0b4de1b295b6ed8c1162c6.png

6.修改Detail组件的展示区(开始变成响应式)

eda45d10fb02406abf145706d912f1c2.png

7.修改News组件的内容(也是变成响应式)

27810a48e0d5422dafb63d856a0516cf.png 展示:(这是标题1的,后面点击其他标题展示区会对应出现内容,就不一一展开了)

6123afb49ebf486fb79cbe435ac9e9ee.png


2.2. params参数的第二种写法

跟query的第二种写法类似,但有一点要区分!!!

cf6ed0aad0d540c7af61b71d2228e3b2.png

注意这里是用name配置项的 ,而不是用path配置项。

那如果我偏用path来配置呢,那就喜提报错!

5e6f267802a6476e9ec08f30fba8b382.png
907476de74794004b87c824db4397c43.png


Detail组件代码:

  1. <template>
  2. <ul class="news-list">
  3. <li>编号:{
  4. { route.params.id }}</li>
  5. <li>标题:{
  6. { route.params.title }}</li>
  7. <li>内容:{
  8. { route.params.content }}</li>
  9. </ul>
  10. </template>
  11. <script setup lang="ts" name="About">
  12. import {useRoute} from 'vue-router'
  13. const route = useRoute()
  14. console.log(route)
  15. </script>
  16. <style scoped>
  17. .news-list {
  18. list-style: none;
  19. padding-left: 20px;
  20. }
  21. .news-list>li {
  22. line-height: 30px;
  23. }
  24. </style>

News组件代码

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

补充:

参数可传可不传的情况,比如:

40d81135f0aa4c17b49e2d3ca3c40651.png

4e2eb30b55ea4bcd91aeb4d4213adb7c.png

f64ba7e1fe464ad2ad0db3e865b17f9d.png

再次提醒:

1.传递params参数时,若使用to的对象写法,必须使用name配置项,不能用path

2.传递params参数时,需要提前在规则中占位。

发表评论

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

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

相关阅读