1. 程式人生 > 實用技巧 >vue跳轉方式

vue跳轉方式

一.router-link

1.不帶引數

<router-link :to="{name:'name'}"></router-link>

<router-link :to="{path:'/name'}"></router-link>

2.帶引數

<router-link :to="{name:'name', params:{id:1}}"></router-link>

<router-link :to="{path:'/name', query:{id:1}}"></router-link>

HTML取參:$route.params.id SCRIPT中取參:this.$route.params.id

二.this.$router.push()

1. 不帶引數

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'}}) 3. params傳參 this.$router.push({name:'home',params: {id:'1'}}) // 只能用 name
4. query和params區別 query類似 get, 跳轉之後頁面 url後面會拼接引數,類似?id=1, 非重要性的可以這樣傳, 密碼之類還是用params,重新整理頁面id還在 params類似 post, 跳轉之後頁面 url後面不會拼接引數 , 但是重新整理頁面id 會消失

三.this.$router.replace()

四.this.$router.go(n)

向前或者向後跳轉n個頁面,n可為正整數或負整數

ps : 區別

this.$router.push

跳轉到指定url路徑,並向history棧中新增一個記錄,點選後退會返回到上一個頁面

this.$router.replace

跳轉到指定url路徑,但是history棧中不會有記錄,點選返回會跳轉到上上個頁面 (就是直接替換了當前頁面)

this.$router.go(n)

向前或者向後跳轉n個頁面,n可為正整數或負整數