vue路由跳轉有四種方式
1. router-link
2. this.$router.push() (函數里面調用)
3. this.$router.replace() (用法同push)
4. this.$router.go(n)
一、不帶參
1.1 router-link
<router-link :to="{name:'home'}"> <router-link :to="{path:'/home'}"> //name,path都行, 建議用name // 注意:router-link中鏈接如果是'/'開始就是從根路由開始,如果開始不帶'/',則從當前路由開始。
1.2 this.$router.push()
this.$router.push('/home') this.$router.push({name:'home'}) this.$router.push({path:'/home'})
二、帶參
2.1 router-link
2.2 this.$router.push(query傳參
this.$router.go(n)
this.$router.go(n) this.$router.go(n) 向前或者向后跳轉n個頁面,n可為正整數或負整數
區別:
this.$router.push 跳轉到指定url路徑,並想history棧中添加一個記錄,點擊后退會返回到上一個頁面
this.$router.replace 跳轉到指定url路徑,但是history棧中不會有記錄,點擊返回會跳轉到上上個頁面 (就是直接替換了當前頁面)
this.$router.go(n) 向前或者向后跳轉n個頁面,n可為正整數或負整數