引用:https://www.cnblogs.com/secretAngel/p/10065611.html
僅僅作為自己筆記使用
項目功能需要,要從列表頁跳轉到詳情頁,需要攜帶參數id,在詳情頁獲取並且使用
1
|
this
.$router.push({name:
'list'
, params:{id: id}})
|
在詳情頁獲取參數:
1
|
this
.$route.params.id
|
試了一下,可以拿到數據,很開心,本以為這樣就結束了,然后刷新了下頁面,發現傳過來的參數丟失了。。。
發現這種傳遞方式刷新頁面參數是會丟失的。
解決辦法: 使用query
this
.$router.push({ name:
'NewsDetail'
, query: { id: id } })
詳情頁獲取參數:
this
.$route.query.id
當然了,大神的文章里也介紹了另一種路由傳參防止刷新參數消失的方法,也是使用最開始使用的那種方法,只是需要在路由中進行一些改動,下面我直接套用大神的代碼說明:
1
2
3
4
5
6
|
routes: [
{
path:
'/list/:id'
,
name:
'list'
}
]
|
1
|
this
.$router.push({name:
'list'
, params:{id: id}});
|
獲取參數:this.$route.params.id即可