Vue路由組件通過props配置傳參的實現

本文主要介紹瞭 Vue路由組件通過props配置傳參的實現,分享給大傢,具體如下:

一、基於params參數傳遞

1、index.js(路由配置)

props:true

// 引入路由
// eslint-disable-next-line no-unused-vars
import VueRouter from 'vue-router'
import Box_1 from '../pages/Box_1.vue'
import Box_2 from '../pages/Box_2.vue'
import Menu_1 from '../pages/Menu_1.vue'
import Menu_2 from '../pages/Menu_2.vue'
 
 
// 創建一個路由器
 
export default new VueRouter({
    routes: [
 
        {
            path: '/Box_1',
            component: Box_1,
            children: [
                {
                    name: 'myMenu',  // 用name代替路徑
                    path: 'Menu_1',
                    component: Menu_1,
                    props:true
                },
                {
                    path: 'Menu_2',
                    component: Menu_2
                },
            ]
        },
        {
            path: '/Box_2',
            component: Box_2,
            children: [
                {
                    path: 'Menu_1',
                    component: Menu_1
                },
                {
                    path: 'Menu_2',
                    component: Menu_2
                },
 
            ]
        },
    ]
 
 
})

2、Box_1.vue(父路由組件 – 發送參數)

<template>
  <div class="m_box">
    <div class="top">
      <!-- 路由跳轉鏈接 -->
      <router-link class="box_1" active-class="active"
        :to="{
          name:'myMenu',
          params:{
            id:id,
            name:name
          }
        }">
        菜單1
      </router-link>
      <!-- 路由跳轉鏈接 -->
      <router-link class="box_2" to="/Box_1/menu_2" active-class="active">
        菜單2
      </router-link>
    </div>
    <div class="bottom">
      <!-- 我是Box_1組件! -->
      <router-view></router-view>
    </div>
  </div>
</template>
 
<script>
export default {
  name: "Box_1",
  data(){
    return {
      id:"666",
      name:"我是Box_1組件傳過來的參數"
    }
  }
};
</script>
 

 3、Menu_1.vue(子路由組件 – 接收參數)

<template>
  <div class="m_box">
    <div class="top">
      <!-- 路由跳轉鏈接 -->
      <router-link class="box_1" active-class="active"
        :to="{
          name:'myMenu',
          params:{
            id:id,
            name:name
          }
        }">
        菜單1
      </router-link>
      <!-- 路由跳轉鏈接 -->
      <router-link class="box_2" to="/Box_1/menu_2" active-class="active">
        菜單2
      </router-link>
    </div>
    <div class="bottom">
      <!-- 我是Box_1組件! -->
      <router-view></router-view>
    </div>
  </div>
</template>
 
<script>
export default {
  name: "Box_1",
  data(){
    return {
      id:"666",
      name:"我是Box_1組件傳過來的參數"
    }
  }
};
</script>
 

二、基於Query和params參數傳遞(通用)

1、index.js(路由配置)

(1)query參數

  • id:$route.query.id,
  • name:$route.query.name,

(2)params參數

  • id:$route.params.id,
  • name:$route.params.name,

// 引入路由
// eslint-disable-next-line no-unused-vars
import VueRouter from 'vue-router'
import Box_1 from '../pages/Box_1.vue'
import Box_2 from '../pages/Box_2.vue'
import Menu_1 from '../pages/Menu_1.vue'
import Menu_2 from '../pages/Menu_2.vue'
 
 
// 創建一個路由器
 
export default new VueRouter({
    routes: [
 
        {
            path: '/Box_1',
            component: Box_1,
            children: [
                {
                    name: 'myMenu',  // 用name代替路徑
                    path: 'Menu_1',
                    component: Menu_1,
                    props($route){
                        return{
                            id:$route.params.id,
                            name:$route.params.name,
                        }
                    }
                },
                {
                    path: 'Menu_2',
                    component: Menu_2
                },
            ]
        },
        {
            path: '/Box_2',
            component: Box_2,
            children: [
                {
                    path: 'Menu_1',
                    component: Menu_1
                },
                {
                    path: 'Menu_2',
                    component: Menu_2
                },
 
            ]
        },
    ]
 
 
})

 2、Box_1.vue(父路由組件 – 發送參數)

註意:params:,如果是想query方式,就改成query

<template>
  <div class="m_box">
    <div class="top">
      <!-- 路由跳轉鏈接 -->
      <router-link class="box_1" active-class="active"
        :to="{
          name:'myMenu',
          params:{
            id:id,
            name:name
          }
        }">
        菜單1
      </router-link>
      <!-- 路由跳轉鏈接 -->
      <router-link class="box_2" to="/Box_1/menu_2" active-class="active">
        菜單2
      </router-link>
    </div>
    <div class="bottom">
      <!-- 我是Box_1組件! -->
      <router-view></router-view>
    </div>
  </div>
</template>
 
<script>
export default {
  name: "Box_1",
  data(){
    return {
      id:"666",
      name:"我是Box_1組件傳過來的參數"
    }
  }
};
</script>

 3、Menu_1.vue(子路由組件 – 接收參數)

<template>
  <div class="m_box">{{id}}.{{name}}</div>
</template>
 
<script>
export default {
  name: "Menu_1",
  props:['id','name'],
  mounted() {
    console.log("=============");
    console.log(this);
  },
};
 
</script>

到此這篇關於Vue路由組件通過props配置傳參的實現的文章就介紹到這瞭,更多相關Vue props傳參內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: