您好,登錄后才能下訂單哦!
本篇內容介紹了“Vue路由組件如何通過props配置傳參”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
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)建一個路由器 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 }, ] }, ] })
<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組件傳過來的參數(shù)" } } }; </script>
<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組件傳過來的參數(shù)" } } }; </script>
(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)建一個路由器 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 }, ] }, ] })
注意: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組件傳過來的參數(shù)" } } }; </script>
<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配置傳參”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。