vue 路由傳參 params 與 query兩種方式的區(qū)別
router文件下index.js里面店乐,是這么定義路由的:
? ?{
? ? ? path:"/detail",
? ? ?name:"detail",
? ? ? component:home
? ? }
我想用params來(lái)傳參聪全,是這么寫(xiě)的四苇,嗯~
?this.$router.push({
?path:"/detail",
?params:{
?name:'nameValue',
?code:10011
? }
});
結(jié)果可想而知装处,接收參數(shù)的時(shí)候:
this.$route.params.code? ? //undefined
這是因?yàn)椋琾arams只能用name來(lái)引入路由挣郭,下面是正確的寫(xiě)法:
this.$router.push({
? name:"detail",
? params:{
? ? name:'nameValue',
? ? code:10011
?}
});
下面整理一下這兩者的差別:
1迄埃、用法上的
剛才已經(jīng)說(shuō)了,query要用path來(lái)引入兑障,params要用name來(lái)引入侄非,接收參數(shù)都是類(lèi)似的,分別是this.$route.query.name和this.$route.params.name流译。
注意接收參數(shù)的時(shí)候逞怨,已經(jīng)是$route而不是$router了哦!福澡!
2叠赦、展示上的
query更加類(lèi)似于我們ajax中g(shù)et傳參,params則類(lèi)似于post革砸,說(shuō)的再簡(jiǎn)單一點(diǎn)除秀,前者在瀏覽器地址欄中顯示參數(shù)糯累,后者則不顯示
query:
params: