vue-router跳轉方式的區(qū)別解析
一、router-link(聲明式路由,在頁面中調用)
在Vue中,router-link稱為聲明式路由,:to綁定為跳轉的目標地址,一種是通過name,另一種是path。
1.1 路由不帶參數
<router-link :to="{ name: 'word' }">路由name方式跳轉首頁</router-link>
<router-link :to="{ path: '/word' }">路由path方式跳轉首頁</router-link>
1.2 路由帶參數跳轉
路由參數的傳遞主要有兩種方式一種是params,另一種是query,主要區(qū)別:
1. params傳參的參數不會顯示在跳轉的URL中,query傳參的參數會顯示在URL中。
2. params所傳的參數通過this.$route.params.參數;獲取,query所傳的參數通過this.$route.query.參數 獲取
3. 因為params所傳遞的參數不顯示在URl中,所以在路由跳轉時推薦params方式進行傳參
4. 都不能傳對象和數組引用類型數據,只能傳字符串類型數據
<router-link :to="{ name: 'home', params: { key: '1', value: '跳轉' } }">路由name,params方式跳轉首頁</router-link>
<router-link :to="{ name: 'home', query: { key: '1', value: '跳轉' } }">路由name,query方式跳轉首頁</router-link>
<router-link :to="{ path: '/home', params: { key: '1', value: '跳轉' } }">路由path,params方式跳轉首頁</router-link>
<router-link :to="{ path: '/home', query: { key: '1', value: '跳轉' } }">路由path,query方式跳轉首頁</router-link>
二、this.$router.push() (在函數里面調用)
2.1不帶參數跳轉
this.$router.push({ path: '/home'});
this.$router.push({ name: 'home'});
2.2帶參數跳轉
<a-button type="primary" @click="goTo">路由name方式跳轉</a-button>
goTo() {
this.$router.push({ name: 'home', params: { a: '1', b: '2' } });//推薦用params傳參方式
this.$router.push({ name: 'home', query: { a: '1', b: '2' } });
}
三、this.$router.resolve()打開新窗口跳轉
3.1通過path形式跳轉
goTo() {
let routeData = this.$router.resolve({
path: '/home',
});
window.open(routeData.href, '_blank');
}
3.2通過name形式跳轉
goTo() {
let routeData = this.$router.resolve({
name: 'home',
});
window.open(routeData.href, '_blank');
}
到此這篇關于vue-router跳轉方式的區(qū)別的文章就介紹到這了,更多相關vue-router跳轉內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
elementplus el-table(行列互換)轉置的兩種方法
本文主要介紹了elementplus el-table(行列互換)轉置,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-06-06

