vue路由傳參的幾種基本方式


原文地址


this.$router.push跳轉

現有如下場景,點擊父組件的li元素跳轉到子組件中,並攜帶參數,便於子組件獲取數據。
父組件中:

<li v-for="article in articles" @click="getDescribe(article.id)">

methods:

方案一:

      getDescribe(id) { // 直接調用$router.push 實現攜帶參數的跳轉 this.$router.push({ path: `/describe/${id}`, })

方案一,需要對應路由配置如下:

   {
     path: '/describe/:id', name: 'Describe', component: Describe }

很顯然,需要在path中添加/:id來對應 $router.push 中path攜帶的參數。在子組件中可以使用來獲取傳遞的參數值。

this.$route.params.id

方案二:

父組件中:通過路由屬性中的name來確定匹配的路由,通過params來傳遞參數。

       this.$router.push({ name: 'Describe', params: { id: id } })

對應路由配置: 注意這里不能使用:/id來傳遞參數了,因為父組件中,已經使用params來攜帶參數了。

 {
     path: '/describe', name: 'Describe', component: Describe }

子組件中: 這樣來獲取參數

this.$route.params.id

方案三:

父組件:使用path來匹配路由,然后通過query來傳遞參數
這種情況下 query傳遞的參數會顯示在url后面?id=?

    this.$router.push({ path: '/describe', query: { id: id } })

對應路由配置:

   {
     path: '/describe', name: 'Describe', component: Describe }

對應子組件: 這樣來獲取參數

this.$route.query.id

下面整理一下params傳參和query傳參的差別:

1、用法上的

  剛才已經說了,query要用path來引入,params要用name來引入,接收參數都是類似的,分別是this.$route.query.name和this.$route.params.name。

注意接收參數的時候,已經是$route而不是$router了哦!!

2、展示上的

  query更加類似於我們ajax中get傳參,params則類似於post,說的再簡單一點,前者在瀏覽器地址欄中顯示參數,后者則不顯示。


原文地址

router-link跳轉

params傳參(url中顯示參數)

文件結構

 


定義路由:在定義path路由路徑時定義參數名和格式,如  path: "/one/login/:num" ,router>index.js文件如下

/* eslint-disable*/

//第一步:引用vue和vue-router ,Vue.use(VueRouter)
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

//第二步:引用定義好的路由組件
import ChildOne from '../components/childOne'
import ChildTwo from '../components/childTwo'
import Log from '../components/log.vue'
import Reg from '../components/reg.vue'

//第三步:定義路由(路由對象包含路由名、路徑、組件、參數、子路由等),每一個路由映射到一個組件
//第四步:通過new Router來創建router實例
export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/one',
      name: 'ChildOne',
      component: ChildOne,
      children:[
        {
          path:'/one/log/:num',
          component:Log,
        },
        {
          path:'/one/reg/:num',
          component:Reg,
        },
      ],
    },
    {
      path: '/two',
      name: 'ChildTwo',
      component: ChildTwo
    }
  ]
})

在父路由組件上使用router-link進行路由導航,傳參用<router-link to="/one/login/001">的形式向子路由組件傳遞參數。使用router-view進行子路由頁面內容渲染,父路由組件childOne.vue 如下:

<template>
  <div style="border:1px solid red;color:red;">
    <p>這是父路由childOne對應的組件頁面</p>
    <p>下面可以點擊顯示嵌套的子路由 </p>
    <router-link to="/one/log/123">顯示登錄頁面</router-link>
    <router-link to="/one/reg/002">顯示注冊頁面</router-link>
    <router-view></router-view>
  </div>
</template>

子路由通過 this.$route.params.num 的形式來獲取父路由向子路由傳遞過來的參數,子路由組件login.vue如下:

<template>
    <div style="border:1px solid orange;color:orange;">
        <p>登錄界面:這是另一個嵌套路由的內容</p>
        <h3>{{this.$route.params.num}}</h3>
    </div>
</template>


效果:

 

 

注意:如上所述路由定義的path: "/one/login/:num"路徑和to="/one/login/001"必須書寫正確,否則不斷點擊切換路由,會因為不斷將傳遞的值顯示添加到url中導致路由出錯,如下:

 

傳遞的值存在url中存在安全風險,為防止用戶修改,另一種方式不在url中顯示傳遞的值

 

params傳參(url中不顯示參數)

定義路由時添加name屬性給映射的路徑取一個別名,router>index.js文件修改router如下:

export default new Router({
    mode: 'history',
    routes: [
        {
            path: '/one',
            name: 'ChildOne',
            component: ChildOne,
            children:[
                {
                    path:'/one/log/',
                    name:'Log',
                    component:Log,
                },
                {
                path:'/one/reg/',
                name:'Reg',
                component:Reg,
                },
            ],
        },
        {
            path: '/two',
            name: 'ChildTwo',
            component: ChildTwo
        }
    ]
})                

在父路由組件上使用router-link進行路由導航,使用   <router-link :to="{name:'home',params:{id:001}}>    形式傳遞參數。注意   ': to= ' 前面的冒號,childOne.vue組件修改如下:

<template>
    <div style="border:1px solid red;color:red;">
        <p>這是父路由childOne對應的組件頁面</p>
        <p>下面可以點擊顯示嵌套的子路由 </p>
        <router-link :to="{name:'Log',params:{num:666}}">顯示登錄頁面</router-link>
        <router-link :to="{name:'Reg',params:{num:888}}">顯示注冊頁面</router-link>
        <router-view></router-view>
    </div>
</template>


子路由組件頁面獲取父路由傳參方式不變,reg.vue 文件如下:

<template>
    <div style="border:1px solid orange;color:orange;">
        <p>登錄界面:這是另一個嵌套路由的內容</p>
        <h3>子路由獲取的參數:{{this.$route.params.num}}</h3>
    </div>
</template>

 

注意:上述這種利用params不顯示url傳參的方式會導致在刷新頁面的時候,傳遞的值會丟失;

 

使用Query實現路由傳參

定義路由 router>index.js文件如下:

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/one',
      name: 'ChildOne',
      component: ChildOne,
      children:[
        {
          path:'/one/log/',
          component:Log,
        },
        {
          path:'/one/reg/',
          component:Reg,
        },
      ],
    },
    {
      path: '/two',
      name: 'ChildTwo',
      component: ChildTwo
    }
  ]
})

  

修改路由導航 <router-link :to="{path:'/one/log',query:{num:123}}"> ,childOne.vue 文件修改如下:

<template>
  <div style="border:1px solid red;color:red;">
    <p>這是父路由childOne對應的組件頁面</p>
    <p>下面可以點擊顯示嵌套的子路由 </p>
    <router-link :to="{path:'/one/log',query:{num:123}}">顯示登錄頁面</router-link>
    <router-link :to="{path:'/one/reg',query:{num:999}}">顯示注冊頁面</router-link>
    <router-view></router-view>
  </div>
</template>

  

子路由組件通過 this.$route.query.num 來顯示傳遞過來的參數,reg.vue 文件如下:

<template>
  <div style="border:1px solid purple;color:purple;">
    <p>注冊界面:這是二級路由頁面</p>
    <h3>{{this.$route.query.num}}</h3>
  </div>
</template>

  

效果如下:


 

 

 

PS: 在第一步的定義路由中我們都使用了mode:history 作用就是去除路由跳轉時路由路徑前的 “#”

常用的mode模式有兩種:

默認為hash模式,最明顯的標志是,URL上有#號 localhost:8080/#/,路由會監聽#后面的信息變化進行路由匹配

另一種為history模式,不會有#出現,很大程度上對URL進行了美化。需要**注意**history模式在打包后的路由跳轉需要服務器配合

默認不使用mode:history 如下

 


返回目錄


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM