欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3路由router.push的使用以及問題分析

 更新時(shí)間:2023年09月13日 15:34:18   作者:Commas.KM  
頁面跳轉(zhuǎn)有很多方法,本次使用的是?vue-router,但卻在使用?router.push?的時(shí)候遇到了點(diǎn)麻煩,所以記錄下來,希望可以幫助有需要的人

一、前言

頁面跳轉(zhuǎn)有很多方法,本次使用的是 vue-router ,但卻在使用 router.push 的時(shí)候遇到了點(diǎn)麻煩,所以記錄下來,希望可以幫助有需要的人。

二、使用方法

首先,我們來了解下 router.push 的使用方法,如下所示:

字符串路徑

// 字符串路徑
router.push('/users/eduardo')

帶有路徑的對象

// 帶有路徑的對象
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' } })

三、問題描述

我使用“命名的路由,并加上參數(shù),讓路由建立 url ”的方式進(jìn)行帶參跳轉(zhuǎn)頁面

// 命名的路由,并加上參數(shù),讓路由建立 url
router.push({ name: 'user', params: { username: 'eduardo' } })

路由定義的JS代碼:

const routes = [
    { path: '/other-page', component: () => import('../views/OtherPage.vue') },    
];

跳轉(zhuǎn)頁面的JS代碼:

try {
  this.$router.push({
    name: 'OtherPage', // 目標(biāo)頁面的路由名稱
    query: {
      param1: 'value1',
      param2: 'value2'
    }
  });
} catch (error) {
  console.log(`router.push.err=${error}`)
}

點(diǎn)擊按鈕進(jìn)行頁面跳轉(zhuǎn),結(jié)果卻失敗,表示匹配不上我設(shè)置的路由,報(bào)錯(cuò)如下:

router.push.err=Error: No match for
 {"name":"OtherPage","query":{"param1":"value1","param2":"value2"},"params":{}}

四、解決方法

既然是匹配不上,那么會(huì)不會(huì)是路由對象中沒有與之匹配的參數(shù),那我打印一下路由看看

示例代碼:

  const routes = this.$router.options.routes;
  console.log(routes)
  const routeNames = routes.map(route => route.name);
  console.log(routeNames);

打印結(jié)果:

原來路由名字是 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)按鈕的頁面 AboutPage.vue

<template>
  <div>
    <h1>About</h1>
    <button @click="jumpToPageWithParam">帶參跳轉(zhuǎn)到其它頁面</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)頁面的路由名稱
          query: {
            param1: 'value1',
            param2: 'value2'
          }
        });
      } catch (error) {
        console.log(`router.push.err=${error}`)
      }
    }
  },
};
</script>
<style scoped>
h1 {
  color: #42b983;
}
</style>

跳轉(zhuǎn)到的頁面 OtherPage.vue

<template>
  <div>
    <h1>接收帶參跳轉(zhuǎn)的頁面</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的使用以及問題分析的詳細(xì)內(nèi)容,更多關(guān)于vue3 router.push的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • vue2路由基本用法實(shí)例分析

    vue2路由基本用法實(shí)例分析

    這篇文章主要介紹了vue2路由基本用法,結(jié)合實(shí)例形式分析了vue2路由基本功能、原理、用法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-03-03
  • vue3中hooks的概述及用法小結(jié)

    vue3中hooks的概述及用法小結(jié)

    這篇文章是介紹一下vue3中的hooks以及它的用法,主要圍繞兩點(diǎn)來介紹,一個(gè)是hooks基本概念,另一個(gè)是vue3中hooks的使用方法,本文結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友參考下吧
    2023-03-03
  • vue點(diǎn)擊按鈕實(shí)現(xiàn)讓頁面的某一個(gè)元素全屏展示

    vue點(diǎn)擊按鈕實(shí)現(xiàn)讓頁面的某一個(gè)元素全屏展示

    這篇文章主要介紹了vue點(diǎn)擊按鈕實(shí)現(xiàn)讓頁面的某一個(gè)元素全屏展示,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 利用Vue Native構(gòu)建移動(dòng)應(yīng)用的全過程記錄

    利用Vue Native構(gòu)建移動(dòng)應(yīng)用的全過程記錄

    VueNative是一個(gè)使用JavaScript構(gòu)建跨平臺(tái)原生移動(dòng)應(yīng)用程序的框架m這篇文章主要給大家介紹了關(guān)于如何利用Vue Native構(gòu)建移動(dòng)應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-08-08
  • vue獲取路由詳細(xì)內(nèi)容信息方法實(shí)例

    vue獲取路由詳細(xì)內(nèi)容信息方法實(shí)例

    獲取路由詳細(xì)內(nèi)容信息是我們?nèi)粘i_發(fā)中經(jīng)常會(huì)遇到的需求,下面這篇文章主要給大家介紹了關(guān)于vue獲取路由詳細(xì)內(nèi)容信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • 基于vue.js仿淘寶收貨地址并設(shè)置默認(rèn)地址的案例分析

    基于vue.js仿淘寶收貨地址并設(shè)置默認(rèn)地址的案例分析

    這篇文章主要介紹了基于vue.js仿淘寶收貨地址并設(shè)置默認(rèn)地址的案例分析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Vue filters過濾器的使用方法

    Vue filters過濾器的使用方法

    這篇文章主要為大家詳細(xì)介紹了Vue filters過濾器的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 詳解vue的數(shù)據(jù)binding綁定原理

    詳解vue的數(shù)據(jù)binding綁定原理

    這篇文章主要介紹了詳解vue的數(shù)據(jù)binding原理介紹,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • vue實(shí)現(xiàn)PC端錄音功能的實(shí)例代碼

    vue實(shí)現(xiàn)PC端錄音功能的實(shí)例代碼

    這篇文章主要介紹了vue實(shí)現(xiàn)PC端錄音功能的實(shí)例代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • vue常用的數(shù)字孿生可視化的自適應(yīng)方案

    vue常用的數(shù)字孿生可視化的自適應(yīng)方案

    這篇文章主要為大家介紹了vue常用的數(shù)字孿生可視化的自適應(yīng)方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07

最新評論