1.不帶參
<router-link :to="{name:'home'}">
<router-link :to="{path:'/home'}">
this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'})
this.$router.replace('/home')
this.$router.replace({name:'home'})
this.$router.replace({path:'/home'})
2.帶參(params)-- params傳參數(shù) (類似post)
//配置path,刷新頁(yè)面id會(huì)保留
// 路由配置 path: "/home/:id" 或者 path: "/home:id"
<router-link :to="{name:'home', params: {id:1}}">
this.$router.push({name:'home',params: {id:'1'}}) // 只能用 name
3.帶參(query)--query傳參數(shù) (類似get,url后面會(huì)顯示參數(shù))
<router-link :to="{name:'home', query: {id:1}}">
this.$router.push({name:'home',query: {id:'1'}})
this.$router.push({path:'/home',query: {id:'1'}})
4. this.$router.replace() (用法同上,push)
5. this.$router.go(n)
向前或者向后跳轉(zhuǎn)n個(gè)頁(yè)面斗锭,n可為正整數(shù)或負(fù)整數(shù)
1.取參
// html 取參 $route.params.id让禀、$route.query.id
// script 取參 this.$route.params.id套么、 this.$route.query.id
2. query和params區(qū)別
query類似 get, 跳轉(zhuǎn)之后頁(yè)面 url后面會(huì)拼接參數(shù),類似?id=1, 刷新頁(yè)面id還在最蕾。非重要性的可以這樣傳, 密碼之類還是用params
params類似 post, 跳轉(zhuǎn)之后頁(yè)面 url后面不會(huì)拼接參數(shù) , 但是刷新頁(yè)面id 會(huì)消失,配置path,刷新頁(yè)面id會(huì)保留
3.區(qū)別
this.$router.push
跳轉(zhuǎn)到指定url路徑娶靡,并想history棧中添加一個(gè)記錄牧牢,點(diǎn)擊后退會(huì)返回到上一個(gè)頁(yè)面
this.$router.replace
跳轉(zhuǎn)到指定url路徑,但是history棧中不會(huì)有記錄姿锭,點(diǎn)擊返回會(huì)跳轉(zhuǎn)到上上個(gè)頁(yè)面 (就是直接替換了當(dāng)前頁(yè)面)