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拿到了。