前言
vue中路由中需要傳遞參數(shù)的話可以用query和param傳遞跪削,兩者類似于get和post。前者地址欄類似xx?p=1后者為xx/1晃跺,且后者可以隱藏地址欄顯示毫玖。其實也可以用vue推薦的vuex進行響應式的參數(shù)管理。
傳遞參數(shù)形式:
兩者傳遞參數(shù)需要router/index.js進行配置:
{
path:'/page1',
name:'page1',
component:page1,
}
,{
// 默認的param傳遞參數(shù)烹玉,顯示地址欄阐滩,刷新數(shù)據(jù)還在
path:'page2/:id',
name:'page2',
component: path:'/page2',,
},{
//query
this.$router.push({path:''/xx',query:{p:1}})
//param
this.$router.push({'name',params:{p:1})
param隱藏參數(shù)
只需要去掉修改path即可址儒,但是刷新會丟失數(shù)據(jù)衅疙,可以通過localstorage解決刷新問題鸳慈。
{
// 刷新丟失數(shù)據(jù)
path:'page2',
name:'page2',
component: path:'/page2',
}