Vue路由組件通過props配置傳參的實(shí)現(xiàn)
本文主要介紹了 Vue路由組件通過props配置傳參的實(shí)現(xiàn),分享給大家,具體如下:

一、基于params參數(shù)傳遞
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'
// 創(chuàng)建一個(gè)路由器
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(父路由組件 - 發(fā)送參數(shù))

<template>
<div class="m_box">
<div class="top">
<!-- 路由跳轉(zhuǎn)鏈接 -->
<router-link class="box_1" active-class="active"
:to="{
name:'myMenu',
params:{
id:id,
name:name
}
}">
菜單1
</router-link>
<!-- 路由跳轉(zhuǎn)鏈接 -->
<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組件傳過來的參數(shù)"
}
}
};
</script>
3、Menu_1.vue(子路由組件 - 接收參數(shù))

<template>
<div class="m_box">
<div class="top">
<!-- 路由跳轉(zhuǎn)鏈接 -->
<router-link class="box_1" active-class="active"
:to="{
name:'myMenu',
params:{
id:id,
name:name
}
}">
菜單1
</router-link>
<!-- 路由跳轉(zhuǎn)鏈接 -->
<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組件傳過來的參數(shù)"
}
}
};
</script>
二、基于Query和params參數(shù)傳遞(通用)
1、index.js(路由配置)
(1)query參數(shù)
- id:$route.query.id,
- name:$route.query.name,
(2)params參數(shù)
- 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'
// 創(chuàng)建一個(gè)路由器
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(父路由組件 - 發(fā)送參數(shù))
注意:params:,如果是想query方式,就改成query

<template>
<div class="m_box">
<div class="top">
<!-- 路由跳轉(zhuǎn)鏈接 -->
<router-link class="box_1" active-class="active"
:to="{
name:'myMenu',
params:{
id:id,
name:name
}
}">
菜單1
</router-link>
<!-- 路由跳轉(zhuǎn)鏈接 -->
<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組件傳過來的參數(shù)"
}
}
};
</script>3、Menu_1.vue(子路由組件 - 接收參數(shù))

<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>到此這篇關(guān)于Vue路由組件通過props配置傳參的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue props傳參內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中的父子組件通訊以及使用sync同步父子組件數(shù)據(jù)
這篇文章主要介紹了Vue中的父子組件通訊以及使用sync同步父子組件數(shù)據(jù),對(duì)vue感興趣的同學(xué),可以參考下2021-04-04
100行代碼實(shí)現(xiàn)vue表單校驗(yàn)功能(小白自編)
這篇文章主要介紹了使用100行代碼實(shí)現(xiàn)vue表單校驗(yàn)功能,本文通過實(shí)例截圖給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
關(guān)于vant的日歷組件,在iPhonex上可選日期空白
這篇文章主要介紹了關(guān)于vant的日歷組件,在iPhonex上可選日期空白,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
在vue.js中使用JSZip實(shí)現(xiàn)在前端解壓文件的方法
今天小編就為大家分享一篇在vue.js中使用JSZip實(shí)現(xiàn)在前端解壓文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09

