vue页面跳转,传参方式大约能够有下面3种状况。html
下面看一下这3者。vue
1、标签跳转及传参app
:to后面能够跟字符串也能够跟对象。ide
<template> <div id="app"> <div><router-link :to="/">首页</router-link></div> <div><router-link :to="{path:'/news/detail',query:{id:1}}">详情</router-link></div> <div><router-link :to="{name:'newsDetail',params:{id:1}}">详情</router-link></div> </div> </template>
页面接参方式以下ui
使用path + 路径,query + 参数。则用this.$route.query.id取值。this
使用name +路由名称,params + 参数。则用this.$route.params.id取值。spa
2、$router.pushcomponent
js控制跳转路由传参以下:router
3、路由组件传参htm
官网地址:https://router.vuejs.org/zh/guide/essentials/passing-props.html
首先:路由部分
let routes = [ { path: '/news', name: 'news', props: true, meta: {}, component: () => import('@/page/news.vue') }, { path: '/newsDetail/:id', name: 'newsDetail', props: true, meta: {}, component: () => import('@/page/newsDetail.vue') } ]
须要加参数的部分参考 newsDetail ,path后面跟占位符,props设置为波尔类型,true。
跳转页面时使用this.$router.push传参。
下面是取值的方式
export default { name: 'HelloWorld', data () { return { msg: '123 } }, props:['id'], mounted(){ console.log(this.$route.params.id, this.id) } }
取值时,方法1:能够直接使用this.$route.params.id取值。
方法2:也能够先放到props中,就能够直接用this.id拿到了。