1.? router-link
1. 不帶參數(shù)
<router-link to="/home">
<router-link :to="{name:'home'}">
<router-link :to="{path:'/home'}"> //name,path都行, 建議用name
// 注意:router-link中鏈接如果是'/'開始就是從根路由開始,如果開始不帶'/'麻敌,則從當(dāng)前路由開始崇决。
2.帶參數(shù)
<router-link :to="{name:'home', params: {id:1}}">
// params傳參數(shù) (類似post)
// 路由配置 path: "/home/:id" 或者 path: "/home:id"
// 不配置path ,第一次可請求,刷新頁面id會(huì)消失
// 配置path,刷新頁面id會(huì)保留
// html 取參 $route.params.id
// script 取參 this.$route.params.id
<router-link :to="{name:'home', query: {id:1}}">
// query傳參數(shù) (類似get,url后面會(huì)顯示參數(shù))
// 路由可不配置,配置路由頁面不跳轉(zhuǎn)
// html 取參 $route.query.id
// script 取參 this.$route.query.id
2.? this.$router.push() (函數(shù)里面調(diào)用)
1. 不帶參數(shù)
this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'})
2. query傳參
this.$router.push({name:'home',query: {id:'1'}})
this.$router.push({path:'/home',query: {id:'1'}})
// html 取參 $route.query.id
// script 取參 this.$route.query.id
3. params傳參
this.$router.push({name:'home',params: {id:'1'}}) // 只能用 name
// 路由配置 path: "/home/:id" 或者 path: "/home:id" ,
// 不配置path ,第一次可請求,刷新頁面id會(huì)消失
// 配置path,刷新頁面id會(huì)保留
// html 取參 $route.params.id
// script 取參 this.$route.params.id
4. query和params區(qū)別
query類似 get, 跳轉(zhuǎn)之后頁面 url后面會(huì)拼接參數(shù),類似?id=1, 非重要性的可以這樣傳, 密碼之類還是用params刷新頁面id還在
params類似 post, 跳轉(zhuǎn)之后頁面 url后面不會(huì)拼接參數(shù) , 但是刷新頁面id 會(huì)消失
3.? this.$router.replace() (用法同上,push)
4.? this.$router.go(n) ()
這個(gè)方法的參數(shù)是一個(gè)整數(shù)波俄,意思是在 history 記錄中向前或者后退多少步豌鸡,類似?
window.history.go(n)炭剪。
向前或者向后跳轉(zhuǎn)n個(gè)頁面每币,n可為正整數(shù)或負(fù)整數(shù)
// 在瀏覽器記錄中前進(jìn)一步,等同于 history.forward()
router.go(1)
// 后退一步記錄嗡害,等同于 history.back()
router.go(-1)
// 前進(jìn) 3 步記錄
router.go(3)
// 如果 history 記錄不夠用焚碌,那就默默地失敗唄
router.go(-100)router.go(100)
ps : 區(qū)別
this.$router.push
跳轉(zhuǎn)到指定url路徑,并想history棧中添加一個(gè)記錄霸妹,點(diǎn)擊后退會(huì)返回到上一個(gè)頁面
this.$router.replace
跳轉(zhuǎn)到指定url路徑十电,但是history棧中不會(huì)有記錄,點(diǎn)擊返回會(huì)跳轉(zhuǎn)到上上個(gè)頁面 (就是直接替換了當(dāng)前頁面)
this.$router.go(n)
向前或者向后跳轉(zhuǎn)n個(gè)頁面叹螟,n可為正整數(shù)或負(fù)整數(shù)