Vue 路由参数传递

news/2024/7/10 2:23:52 标签: vue, 路由

文章目录

上一节:Vue 路由的基本使用

路由的query参数

先看效果
在这里插入图片描述
我们需要创建一个 Detail.vue 组件,根据点击的消息来展示消息id和标题

创建路由规则,修改 index.js

......
import Detail from "../pages/Detail";
//创建并暴露一个路由
export default new VueRouter({
    routes: [
        {......},
        {
            path: '/home',
            component: Home,
            children: [
                {......},
                {
                    path: 'message',
                    component: Message,
                    children: [
                        {
                            path: 'detail',
                            component: Detail
                        }
                    ]
                }
            ]
        },
    ]
})

修改 Message.vue 组件

<template>
  <div>
    <ul>
      <li v-for="m in messageList" :key="m.id">
        <!--跳转路由并携带query参数,to的字符串写法-->
        <!--<router-link :to="`/home/message/detail?id=${m.id}&title=${m.title}`">{{m.title}}</router-link>-->
        <!--跳转路由并携带query参数,to的对象写法-->
        <router-link :to="{
          path:'/home/message/detail',
          query:{
            id:m.id,
            title:m.title
          }
        }">
          {{ m.title }}
        </router-link>
      </li>
    </ul>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: "Message",
  data() {
    return {
      messageList: [
        {id: "001", title: "消息001"},
        {id: "002", title: "消息002"},
        {id: "003", title: "消息003"},
      ]
    }
  }
}
</script>

创建 Detail.vue 组件

<template>
<ul>
  <li>消息编号:{{$route.query.id}}</li>
  <li>消息标题:{{$route.query.title}}</li>
</ul>
</template>

<script>
export default {
  name: "Detail"
}
</script>

小结
1.传递参数

<!-- 跳转并携带query参数,to的字符串写法-->
<router-link :to=" /home/message/detail?id=666&title=你好">跳转</router-link>
<!--跳转并携带query参数,to的对象写法-->
<router-link
:to="{
	path:'/home/message/detail',
	query:{
		id:666,
		title:'你好'
	}"
}>
跳转
</router-link>

2.接收参数

$route.query.id
$route.query.title

命名路由

命名路由就是给路由起名字,可以简化代码,增加 name 属性即可。修改 index.js,给 Detail 增加 name 属性:
在这里插入图片描述
在 Message 跳转 Detail 时,代码可以简化为:

<router-link :to="{
          name:'xiangqing',
          query:{
            id:m.id,
            title:m.title
          }
        }">

index.js 中给 about 增加 name 属性
在这里插入图片描述
App.vue

<!--          <router-link class="list-group-item" active-class="active" to="/about">About</router-link>-->
<router-link class="list-group-item" active-class="active" :to="{name:'guanyu'}">About</router-link>

路由的params参数

首先需要配置 index.js,使用占位符 params 参数

export default new VueRouter({
    routes: [
        {......},
        {
            path: '/home',
            component: Home,
            children: [
                {......},
                {
                    path: 'message',
                    component: Message,
                    children: [
                        {
                            name:'xiangqing',
                            path: 'detail/:id/:title',
                            component: Detail
                        }
                    ]
                }
            ]
        },
    ]
})

Message.vue 中传值,使用字符串写法:

<!--跳转路由并携带params参数,to的字符串写法-->
<router-link :to="`/home/message/detail/${m.id}/${m.title}`">{{m.title}}</router-link>

或者使用对象写法

特别注意:路由携带params参数时,若使用to的对象写法,则不能使用path配置项,必须使用name配置!

<!--跳转路由并携带params参数,to的对象写法-->
        <router-link :to="{
          name:'xiangqing',
          params:{
            id:m.id,
            title:m.title
          }
        }">
          {{ m.title }}
        </router-link>

Detail 中取值

  <li>消息编号:{{$route.params.id}}</li>
  <li>消息标题:{{$route.params.title}}</li>

点击消息002时:
在这里插入图片描述

props配置

我们关注一个问题,在 Detail 中,我们接收参数时使用 $route.params.id$route.params.title,如果参数很多,会重复写很多次 $route.params.xxx,为了解决这个问题,就用到了 props 配置

修改 index.js,哪里接收参数就在哪里配置

第一种写法

children: [
	{
		name:'xiangqing',
        path: 'detail/:id/:title',
        component: Detail,
        //props的第一种写法,值为对象,该对象中的所有key-value都会以props的形式传给Detail组件。
        props:{
        	a:1,
            b:"hello"
        }
    }
]

接收时使用 props 接收,使用插值语法读出来即可

<template>
<ul>
  <li>{{a}}{{b}}</li>
</ul>
</template>

<script>
export default {
  name: "Detail",
  props:['a','b']
}
</script>

第二种写法

修改 index.js

children: [
	{
		name:'xiangqing',
        path: 'detail/:id/:title',
        component: Detail,
		//props的第二种写法,值为布尔值
		//若布尔值为真,就会把该路由组件收到的所有params参数,以props的形式传给Detail组件
        props:true
    }
]

Detail 接收时

<template>
<ul>
  <li>消息编号:{{id}}</li>
  <li>消息标题:{{title}}</li>
</ul>
</template>

<script>
export default {
  name: "Detail",
  props:["id","title"]
}
</script>

第三种写法

第二种写法只适合 params 传参。我们看下第三种写法适合 query。修改 index.js

children: [
	{
		name:'xiangqing',
        path: 'detail/',
        component: Detail,
		//props的第三种写法,值为函数
        props($route){
           return {id:$route.query.id,title:$route.query.title}
        }
        //或简写为结构赋值
        props({query}){
        	return {id:query.id,title:query.title}
        }
        //或接着结构赋值
        props({query: {id, title}}) {
        	return {id, title}
        }
    }
]

Message.vue 中使用 query 传参

<router-link :to="{
          name:'xiangqing',
          query:{
            id:m.id,
            title:m.title
          }
        }">
          {{ m.title }}
</router-link>

Detail 中接收时仍是同上面一样

<template>
<ul>
  <li>消息编号:{{id}}</li>
  <li>消息标题:{{title}}</li>
</ul>
</template>

<script>
export default {
  name: "Detail",
  props:["id","title"]
}
</script>

小结:路由的props配置
作用:让路由组件更方便的收到参数

{
	name : 'xiangqing',
	path : 'detail/:id',
	component: Detail,
	//第一种写法: props值为对象,该对象中所有的key-value的组合最终都会通过props传给Detail组件
	//props:{a: 900}
	//第二种写法: props值为布尔值,布尔值为true,则把路由收到的所有params参数通过props传给Detail组件
	// props :true
	//第三种写法:props值为函数,该函数返回的对象中每一组key-value都会通过props传给Detail组件
	props(route){
		return {
			id : route.query.id,
			title:route.query.title
		}
	}
}

下一节:Vue 编程式路由导航


http://www.niftyadmin.cn/n/1816706.html

相关文章

Cookie 基本操作

Cookie 基本操作 对于 Cookie 得常用操作有&#xff0c;存取&#xff0c;读取&#xff0c;以及设置有效期&#xff1b;具体可以参照 JavaScript 操作 Cookie 一文&#xff1b;但&#xff0c;近期在前端编码方面&#xff0c;皆以Vue为冲锋利器&#xff0c;所以就有用到一款插件 …

IBatisNet配置

结合上面示例中的IbatisNet配置文件&#xff0c;下面对配置文件中各节点的说明&#xff1a; <?xml version"1.0" encoding"utf-8"?><sqlMapConfig xmlns"http://ibatis.apache.org/dataMapper" xmlns:xsihttp://www.w3.org/2001/XMLS…

Vue 编程式路由导航

文章目录router- link的replace属性编程式路由导航缓存路由组件两个新的生命钩子router- link的replace属性 1.作用&#xff1a;控制路由跳转时操作浏览器历史记录的模式 2.浏览器的历史记录有两种写入方式&#xff1a;分别为push和replace&#xff0c;push是追加历史记录&…

嘿嘿,看看你属于哪个阶段的程序员? (转)

国外开发者博客中有一篇有趣的文章&#xff0c;将程序员按水平像软件版本号那样划分为不同的版本。相对于在招聘时分为初级&#xff0c;中级&#xff0c;高级程序员&#xff0c;直接表明 需要某种语言N版本的程序员或许更方便直接。根据作者的观点&#xff0c;可将WEB开发者大致…

[导入]GEF中导视图的使用

作者: liugang594 链接&#xff1a;http://liugang594.javaeye.com/blog/213545 发表时间: 2008年07月10日 声明&#xff1a;本文系JavaEye网站发布的原创博客文章&#xff0c;未经作者书面许可&#xff0c;严禁任何网站转载本文&#xff0c;否则必将追究法律责任&#xff01…

JavaScript调节透明度

动态改变照片的透明度 <img id"imgInfo" src"Images/Train.jpg" style"filter: alpha(opacity50)"/> <input id"Button1" type"button" value"透明度" οnclick"rdl_change(1)" /> …

HTML5上传图片预览

笔记一下&#xff01;&#xff01;&#xff01; <!DOCTYPE html> <html> <head> <title>HTML5上传图片预览</title> <meta http-equiv"Content-Type" content"text/html; charsetUTF-8"> <script src"http://…

Vue Element-UI使用

文章目录UI组件库介绍Element UI 使用UI组件库介绍 移动端常用UI组件库 VantCube UlMint UI PC端常用U组件库 Element UllView Ul Element UI 使用 全部引入 我们下面来演示一下 Element UI 的使用&#xff0c;它的 开发指南&#xff0c;根据步骤来做就可以了&#xff0c…