vue3路由router.push的使用以及問(wèn)題分析
一、前言
頁(yè)面跳轉(zhuǎn)有很多方法,本次使用的是 vue-router ,但卻在使用 router.push 的時(shí)候遇到了點(diǎn)麻煩,所以記錄下來(lái),希望可以幫助有需要的人。
二、使用方法
首先,我們來(lái)了解下 router.push 的使用方法,如下所示:
字符串路徑
// 字符串路徑
router.push('/users/eduardo')帶有路徑的對(duì)象
// 帶有路徑的對(duì)象
router.push({ path: '/users/eduardo' })帶查詢參數(shù),結(jié)果是 /register?plan=private
// 帶查詢參數(shù),結(jié)果是 /register?plan=private
router.push({ path: '/register', query: { plan: 'private' } })帶 hash,結(jié)果是 /about#team
// 帶 hash,結(jié)果是 /about#team
router.push({ path: '/about', hash: '#team' })命名的路由,并加上參數(shù),讓路由建立 url
// 命名的路由,并加上參數(shù),讓路由建立 url
router.push({ name: 'user', params: { username: 'eduardo' } })三、問(wèn)題描述
我使用“命名的路由,并加上參數(shù),讓路由建立 url ”的方式進(jìn)行帶參跳轉(zhuǎn)頁(yè)面
// 命名的路由,并加上參數(shù),讓路由建立 url
router.push({ name: 'user', params: { username: 'eduardo' } })路由定義的JS代碼:
const routes = [
{ path: '/other-page', component: () => import('../views/OtherPage.vue') },
];跳轉(zhuǎn)頁(yè)面的JS代碼:
try {
this.$router.push({
name: 'OtherPage', // 目標(biāo)頁(yè)面的路由名稱
query: {
param1: 'value1',
param2: 'value2'
}
});
} catch (error) {
console.log(`router.push.err=${error}`)
}點(diǎn)擊按鈕進(jìn)行頁(yè)面跳轉(zhuǎn),結(jié)果卻失敗,表示匹配不上我設(shè)置的路由,報(bào)錯(cuò)如下:
router.push.err=Error: No match for
{"name":"OtherPage","query":{"param1":"value1","param2":"value2"},"params":{}}四、解決方法
既然是匹配不上,那么會(huì)不會(huì)是路由對(duì)象中沒(méi)有與之匹配的參數(shù),那我打印一下路由看看
示例代碼:
const routes = this.$router.options.routes; console.log(routes) const routeNames = routes.map(route => route.name); console.log(routeNames);
打印結(jié)果:

原來(lái)路由名字是 undefined ,瞬間知道解決方法,給路由起個(gè)名字 name:'OtherPage' ,路由定義的JS代碼如下所示:
{ path: '/other-page',name:'OtherPage', component: () => import('../views/OtherPage.vue') },再次點(diǎn)擊跳轉(zhuǎn),完美手工

五、完整代碼
路由定義 router.js
import { createRouter, createWebHistory } from 'vue-router';
// NO1 和 NO2 兩種寫法均可
// import AboutPage from '../views/AboutPage.vue';
// import OtherPage from '../views/OtherPage.vue';
const routes = [
// NO1:
// { path: '/', name: 'AboutPage', component: AboutPage },
// { path: '/', name: 'OtherPage', component: OtherPage},
// NO2:
{ path: '/about', name: 'AboutPage', component: () => import('../views/AboutPage.vue') },
{ path: '/other-page', name: 'OtherPage', component: () => import('../views/OtherPage.vue') },
];
const router = createRouter({
history: createWebHistory(),
routes,
});
// export default router;
export { router, routes } //router是主路由,routes是子路由有跳轉(zhuǎn)按鈕的頁(yè)面 AboutPage.vue
<template>
<div>
<h1>About</h1>
<button @click="jumpToPageWithParam">帶參跳轉(zhuǎn)到其它頁(yè)面</button>
</div>
</template>
<script>
export default {
name: 'AboutPage',
methods: {
async jumpToPageWithParam() {
// const routes = this.$router.options.routes;
// console.log(routes)
// const routeNames = routes.map(route => route.name);
// console.log(routeNames);
try {
this.$router.push({
name: 'OtherPage', // 目標(biāo)頁(yè)面的路由名稱
query: {
param1: 'value1',
param2: 'value2'
}
});
} catch (error) {
console.log(`router.push.err=${error}`)
}
}
},
};
</script>
<style scoped>
h1 {
color: #42b983;
}
</style>跳轉(zhuǎn)到的頁(yè)面 OtherPage.vue
<template>
<div>
<h1>接收帶參跳轉(zhuǎn)的頁(yè)面</h1>
</div>
</template>
<script>
export default {
name: 'AboutPage',
mounted() {
// 獲取查詢參數(shù)
const param1 = this.$route.query.param1;
const param2 = this.$route.query.param2;
// 使用參數(shù)執(zhí)行操作
console.log('param1:', param1);
console.log('param2:', param2);
}
};
</script>
<style scoped>
h1 {
color: #42b983;
}
</style>以上就是vue3路由router.push的使用以及問(wèn)題分析的詳細(xì)內(nèi)容,更多關(guān)于vue3 router.push的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue如何在CSS中使用data定義的數(shù)據(jù)淺析
這篇文章主要給大家介紹了關(guān)于Vue如何在CSS中使用data定義的數(shù)據(jù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-05-05
Vue?+?Android?WebView實(shí)現(xiàn)大文件PDF預(yù)覽完整解決方案(附詳細(xì)代碼)
這篇文章主要介紹了Vue?+?Android?WebView實(shí)現(xiàn)大文件PDF預(yù)覽完整解決方案的相關(guān)資料,解決了在AndroidWebView環(huán)境下預(yù)覽大文件PDF的問(wèn)題,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-12-12
vue3簡(jiǎn)易實(shí)現(xiàn)proxy代理實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03
Vue.js中父組件調(diào)用子組件的內(nèi)部方法詳解
在Vue.js開(kāi)發(fā)中,組件化架構(gòu)使得代碼更模塊化和可維護(hù),但有時(shí)父組件需要直接調(diào)用子組件的內(nèi)部方法,例如觸發(fā)子組件的特定功能或狀態(tài)更新,本文將重點(diǎn)講解如何使用refs實(shí)現(xiàn)這一需求,需要的朋友可以參考下2025-11-11
vue 數(shù)據(jù)雙向綁定的實(shí)現(xiàn)方法
這篇文章主要介紹了vue 數(shù)據(jù)雙向綁定的實(shí)現(xiàn)方法,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-03-03
基于Vue2的獨(dú)立構(gòu)建與運(yùn)行時(shí)構(gòu)建的差別(詳解)
下面小編就為大家分享一篇基于Vue2的獨(dú)立構(gòu)建與運(yùn)行時(shí)構(gòu)建的差別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12

