Vue-CLI项目中如何实现路由长尾词参数传递的方法有哪些?

2026-04-08 14:211阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计555个文字,预计阅读时间需要3分钟。

Vue-CLI项目中如何实现路由长尾词参数传递的方法有哪些?

一、标签传参方式:`router-link` 或 `router-link`

第一种:使用 `router.js` 定义路由javascript{ path: '/course/detail/:id', name: 'course-detail', component: CourseDetail}

第二种:在 `card` 的内容中传递参数

一.标签传参方式:<router-link></router-link>

第一种

router.js { path: '/course/detail/:pk', name: 'course-detail', component: CourseDetail }

传递层

<!-- card的内容 { id: 1, bgColor: 'red', title: 'Python基础' } --> <router-link :to="`/course/detail/${card.id}`">详情页</router-link>

接收层

let id = this.$route.params.pk

演变体

""" { path: '/course/:pk/:name/detail', name: 'course-detail', component: CourseDetail } <router-link :to="`/course/${card.id}/${card.title}/detail`">详情页</router-link> let id = this.$route.params.pk let title = this.$route.params.name """

第二种

router.js { // 浏览器链接显示:/course/detail,注:课程id是通过数据包方式传递 path: '/course/detail', name: 'course-detail', component: CourseDetail }

传递层

<!-- card的内容 { id: 1, bgColor: 'red', title: 'Python基础' } --> <router-link :to="{ name: 'course-detail', params: {pk: card.id} }">详情页</router-link>

接收层

let id = this.$route.params.pk

第三种

router.js { // 浏览器链接显示:/course/detail?pk=1,注:课程id是通过路由拼接方式传递 path: '/course/detail', name: 'course-detail', component: CourseDetail }

传递层

Vue-CLI项目中如何实现路由长尾词参数传递的方法有哪些?

<!-- card的内容 { id: 1, bgColor: 'red', title: 'Python基础' } --> <router-link :to="{ name: 'course-detail', query: {pk: card.id} }">详情页</router-link>

接收层

let id = this.$route.query.pk

二.逻辑传参:this.$router

第一种

""" 路由: path: '/course/detail/:pk'

跳转:id是存放课程id的变量

this.$router.push(`/course/detail/${id}`)

接收:

let id = this.$route.params.pk """

第二种

""" 路由: path: '/course/detail' 跳转:id是存放课程id的变量 this.$router.push({ 'name': 'course-detail', params: {pk: id} }); 接收: let id = this.$route.params.pk """

第三种

""" 路由: path: '/course/detail' 跳转:id是存放课程id的变量 this.$router.push({ 'name': 'course-detail', query: {pk: id} }); 接收: let id = this.$route.query.pk """

总结

以上所述是小编给大家介绍的Vue-CLI项目中路由传参的方式详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

本文共计555个文字,预计阅读时间需要3分钟。

Vue-CLI项目中如何实现路由长尾词参数传递的方法有哪些?

一、标签传参方式:`router-link` 或 `router-link`

第一种:使用 `router.js` 定义路由javascript{ path: '/course/detail/:id', name: 'course-detail', component: CourseDetail}

第二种:在 `card` 的内容中传递参数

一.标签传参方式:<router-link></router-link>

第一种

router.js { path: '/course/detail/:pk', name: 'course-detail', component: CourseDetail }

传递层

<!-- card的内容 { id: 1, bgColor: 'red', title: 'Python基础' } --> <router-link :to="`/course/detail/${card.id}`">详情页</router-link>

接收层

let id = this.$route.params.pk

演变体

""" { path: '/course/:pk/:name/detail', name: 'course-detail', component: CourseDetail } <router-link :to="`/course/${card.id}/${card.title}/detail`">详情页</router-link> let id = this.$route.params.pk let title = this.$route.params.name """

第二种

router.js { // 浏览器链接显示:/course/detail,注:课程id是通过数据包方式传递 path: '/course/detail', name: 'course-detail', component: CourseDetail }

传递层

<!-- card的内容 { id: 1, bgColor: 'red', title: 'Python基础' } --> <router-link :to="{ name: 'course-detail', params: {pk: card.id} }">详情页</router-link>

接收层

let id = this.$route.params.pk

第三种

router.js { // 浏览器链接显示:/course/detail?pk=1,注:课程id是通过路由拼接方式传递 path: '/course/detail', name: 'course-detail', component: CourseDetail }

传递层

Vue-CLI项目中如何实现路由长尾词参数传递的方法有哪些?

<!-- card的内容 { id: 1, bgColor: 'red', title: 'Python基础' } --> <router-link :to="{ name: 'course-detail', query: {pk: card.id} }">详情页</router-link>

接收层

let id = this.$route.query.pk

二.逻辑传参:this.$router

第一种

""" 路由: path: '/course/detail/:pk'

跳转:id是存放课程id的变量

this.$router.push(`/course/detail/${id}`)

接收:

let id = this.$route.params.pk """

第二种

""" 路由: path: '/course/detail' 跳转:id是存放课程id的变量 this.$router.push({ 'name': 'course-detail', params: {pk: id} }); 接收: let id = this.$route.params.pk """

第三种

""" 路由: path: '/course/detail' 跳转:id是存放课程id的变量 this.$router.push({ 'name': 'course-detail', query: {pk: id} }); 接收: let id = this.$route.query.pk """

总结

以上所述是小编给大家介绍的Vue-CLI项目中路由传参的方式详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!