1.query方式傳參和接收參數(shù)
傳參:
this.$router.push({
path:'/xxx'
query:{
id:id
}
})
接收參數(shù):
this.$route.query.id
2.params方式傳參和接收參數(shù)
傳參: (用name來傳的時候 路由那里要配置一下name)
this.$router.push({
name:'xxx'
params:{
id:id
}
})
接收參數(shù):
this.$route.params.id
注意:params傳參,push里面只能是 name:'xxxx',不能是path:'/xxx',因為params只能用name來引入路由,如果這里寫成了path,接收參數(shù)頁面會是undefined?。?!
另外,二者還有點區(qū)別,直白的來說query相當于get請求,頁面跳轉的時候,可以在地址欄看到請求參數(shù),而params相當于post請求,參數(shù)不會再地址欄中顯示
注意:傳參是this.$router,接收參數(shù)是this.$route,這里千萬要看清了?。?!
3. this.$router 和this.$route有何區(qū)別?
1.$router為VueRouter實例,想要導航到不同URL,則使用$router.push方法
2.$route為當前router跳轉對象,里面可以獲取name、path、query、params等