vue路由跳轉有四種方式
1. router-link
2. this.$router.push() (函數(shù)里面調用)
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'})
1.3 this.$router.replace() (用法同push)
二、帶參
2.1 router-link
<router-link :to="{name:'home', params: {id:1}}">
// params傳參數(shù) (類似post)
// 路由配置 path: "/home/:id" 或者 path: "/home:id"
// 不配置path ,第一次可請求,刷新頁面id會消失
// 配置path,刷新頁面id會保留
// html 取參? $route.params.id
// script 取參? this.$route.params.id
<router-link :to="{name:'home', query: {id:1}}">
// query傳參數(shù) (類似get,url后面會顯示參數(shù))
// 路由可不配置
// html 取參? $route.query.id
// script 取參? this.$route.query.id
2.2 this.$router.push(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
params傳參
this.$router.push({name:'home',params: {id:'1'}})? // 只能用 name
// 路由配置 path: "/home/:id" 或者 path: "/home:id" ,
// 不配置path ,第一次可請求,刷新頁面id會消失
// 配置path,刷新頁面id會保留
// html 取參? $route.params.id
// script 取參? this.$route.params.id
2.3 this.$router.replace() (用法同push)
this.$router.go(n)
this.$router.go(n) this.$router.go(n) 向前或者向后跳轉n個頁面盐茎,n可為正整數(shù)或負整數(shù)
區(qū)別:
this.$router.push 跳轉到指定url路徑涧衙,并想history棧中添加一個記錄唤殴,點擊后退會返回到上一個頁面
this.$router.replace 跳轉到指定url路徑哺窄,但是history棧中不會有記錄捐下,點擊返回會跳轉到上上個頁面 (就是直接替換了當前頁面)
this.$router.go(n) 向前或者向后跳轉n個頁面,n可為正整數(shù)或負整數(shù)
如果我的內容能對你有所幫助萌业,我就很開心啦蔑担!