VUE中使用路由router跳轉(zhuǎn)頁面多種方式
前言
在 Vue 中,this.$router.push 是用來跳轉(zhuǎn)到另一個路由的方法,它可以傳遞參數(shù)。Vue Router 提供了多種方式來傳遞路由參數(shù),常見的有 查詢參數(shù)(query) 和 路由參數(shù)(params)。
1. 查詢參數(shù)(Query Parameters)
查詢參數(shù)是 URL 中 ? 后面的部分,格式通常是 key=value。查詢參數(shù)的優(yōu)勢是可以在 URL 中顯示,且不依賴于路由的定義。
路由跳轉(zhuǎn)并傳遞查詢參數(shù)
可以使用 this.$router.push 跳轉(zhuǎn)到一個新的路由,并在 URL 中添加查詢參數(shù)。
// 路由跳轉(zhuǎn),并傳遞查詢參數(shù) this.$router.push({ path: '/newPage', query: { id: 123, name: 'Tom' } });
這里,/newPage?id=123&name=Tom 是最終跳轉(zhuǎn)后的 URL??梢詡鬟f一個對象,指定 path(目標(biāo)路徑)和 query(查詢參數(shù))。
在目標(biāo)頁面中獲取查詢參數(shù)
在目標(biāo)頁面中,可以通過 this.$route.query 來獲取查詢參數(shù)。
// 獲取查詢參數(shù) const id = this.$route.query.id; // 123 const name = this.$route.query.name; // 'Tom'
2. 路由參數(shù)(Route Params)
路由參數(shù)是 URL 路徑的一部分,通常在路由定義中使用動態(tài)路由(例如:/user/:id)。路由參數(shù)通常是隱藏在 URL 中的。
路由跳轉(zhuǎn)并傳遞路由參數(shù)
可以在 this.$router.push 中通過 params 傳遞路由參數(shù)。
// 路由跳轉(zhuǎn),并傳遞路由參數(shù) this.$router.push({ name: 'user', params: { lsls: 123, namez: 'Tom' } });
這里,/user/123 是跳轉(zhuǎn)后的 URL,id 就是路由參數(shù)。需要確保路由已經(jīng)在路由配置中定義了動態(tài)參數(shù)。
路由配置示例(帶參數(shù))
const routes = [ { path: '/user/:lsls/:namez',//可定義多個參數(shù) name: 'user', component: UserPage } ];
在目標(biāo)頁面中獲取路由參數(shù)在目標(biāo)頁面中,可以通過 this.$route.params 獲取路由參數(shù)。
// 獲取路由參數(shù) const id = this.$route.params.lsls; // 123 const id = this.$route.params.namez; // Tom
注意事項:
路由參數(shù)(params) 只能通過 命名路由 或 動態(tài)路由 進行傳遞(/user/:id。如果在使用 query 參數(shù)時,路徑會顯示為?key=value,而 params 會直接出現(xiàn)在路徑中,比如 /user/123。query 和 params 傳遞的參數(shù)方式不同,query 用于查詢字符串,params 用于路徑參數(shù)。 在組件的 mounted 或 created 生命周期鉤子中,你可以訪問 this.$route 來獲取當(dāng)前路由的參數(shù),無論是 query 還是 params。
- 避免使用 params 和 query 混合使用
- 在訪問路由參數(shù)時(如 this.r o u t e . p a r a m s 或 t h i s . route.params 或 this.route.params或this.route.query),需要注意你訪問它們的時機。如果在組件加載前(如 created 鉤子)訪問,可能還無法獲取到最新的路由信息。此時,可以通過 beforeRouteEnter 或 beforeRouteUpdate 鉤子來獲取更準(zhǔn)確的信息。
beforeRouteEnter(to, from, next) { // 在進入頁面時獲取路由參數(shù) console.log(to.params.id); next(); } beforeRouteUpdate(to, from, next) { // 在路由變化時獲取更新后的路由參數(shù) console.log(to.params.id); next(); }
- 如果路由配置中使用了重定向(redirect),并且重定向的目標(biāo)路徑中包含參數(shù),確保參數(shù)在重定向過程中正確傳遞。
const routes = [ { path: '/oldPage', redirect: { name: 'newPage', params: { id: 123 } } }, { path: '/newPage/:id', name: 'newPage', component: NewPage } ]; // 如果 /oldPage 被訪問,將會重定向到 /newPage/123
- 如果使用了動態(tài)路由,并且在路由中傳遞了 params,但目標(biāo)組件沒有相關(guān)的邏輯處理該參數(shù)時,可能會遇到問題。建議在組件中使用默認(rèn)值,確保如果某些參數(shù)沒有傳遞時,頁面能夠正常工作。
const id = this.$route.params.id || 'defaultId';
注:
- params 用于路徑參數(shù),適用于動態(tài)路由;query 用于查詢字符串參數(shù),通常用于 URL 中顯示。
- 確保路由配置與傳遞的參數(shù)一致,避免混淆 params 和 query。
- 在訪問路由參數(shù)時,注意訪問時機,推薦使用生命周期鉤子如beforeRouteEnter,beforeRouteUpdate 處理路由變化。
- 在跳轉(zhuǎn)時,確保不混用兩種參數(shù),保持清晰的參數(shù)傳遞方式。
完整示例Vue 應(yīng)用,路由配置如下:
import Vue from 'vue'; import Router from 'vue-router'; import UserPage from './components/UserPage'; Vue.use(Router); const routes = [ { path: '/user/:id/:name?', // 定義兩個動態(tài)參數(shù) id 和 name 可以使用?標(biāo)識參數(shù)可選傳遞 name: 'user', component: UserPage } ]; const router = new Router({ routes }); export default router;
在跳轉(zhuǎn)時,可以使用如下代碼:
// 跳轉(zhuǎn)到 /user/123/Tom this.$router.push({ name: 'user', params: { id: 123, name: 'Tom' } });
然后在 UserPage 組件中,可以獲取到 id 和 name 參數(shù):
export default { created() { const id = this.$route.params.id; const name = this.$route.params.name;// 如果可選參數(shù)沒傳則是undefind console.log(`User ID: ${id}, Name: ${name}`); } };
順序:確保在路由定義中,參數(shù)的順序與跳轉(zhuǎn)時傳遞參數(shù)的順序一致。
可選參數(shù):如果你希望某些參數(shù)是可選的,可以在路由配置中使用 ? 來標(biāo)記這些參數(shù)為可選。
總結(jié)
到此這篇關(guān)于VUE中使用路由router跳轉(zhuǎn)頁面多種方式的文章就介紹到這了,更多相關(guān)vue用路由router跳轉(zhuǎn)頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3+Canvas實現(xiàn)坦克大戰(zhàn)游戲(二)
本文主要給大家講解一下子彈擊中物體、物體銷毀、敵方坦克構(gòu)建生成、運動算法、爆炸效果、以及障礙物的生成,感興趣的小伙伴可以了解一下2022-03-03Vue中el-tree樹全部展開或收起的實現(xiàn)示例
本文主要介紹了Vue中el-tree樹全部展開或收起的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07