[英]multiple different nested routes in same component based on conditional switch case - vue
I am implementing a players module.我正在实现一个播放器模块。 In here, there are 3 routing cards in initial page.
在这里,初始页面中有 3 个路由卡。 Each card is routing to next page which is having multiple routing cards of their respective parent card.
每张卡都路由到具有各自父卡的多个路由卡的下一页。
Example例子
1. 1st level Card( Player Name) -- > 2nd level Cards(Players info, Player Statistics, Records) --> upto 3rd level routing
2. 1st level Card(Grounds) -- > 2nd level Cards(Grounds info, Match played, Records) --> upto 3rd level routing
3. 1st level Card(Match Information) -- > 2nd level Cards(Match info, match history, community resources) --> upto 3rd level routing
Now, i have created that many components with respective routes.现在,我已经创建了许多具有各自路线的组件。 But, i want to create one component to route all different routes based on condition( $route.path)
但是,我想创建一个组件来根据条件路由所有不同的路由($route.path)
Now, here is my routes.js现在,这是我的 routes.js
{
name: 'Players',
path: '/players',
component: PageLayout,
meta: {
topNavigation: true
},
redirect: { path: '/players/entry' },
children: [
{
name: 'PlayersEntryPage',
path: 'entry',
component: PlayersEntryPage
},
{
name: 'PlayersName',
path: 'name',
component: PlayersName
},
{
name: 'Grounds',
path: 'grounds',
component: Grounds
},
{
name: 'MatchInformation',
path: 'info',
component: MatchInformation
}
]
}
Here is the first home page and then 1st level subsequent card components for routing这是第一个主页,然后是用于路由的 1 级后续卡组件
PlayersEntryPage PlayersEntryPage
<template>
<vu-page background="gray">
<vu-container>
<vu-row-control class="mt-3">
<vu-box-control :list="cardLists" @click="cardAction"></vu-box-control>
</vu-row-control>
</vu-container>
</vu-page>
</template>
<script>
export default {
data() {
return {
cardLists: [
{ heading: ‘PlName', content:’Players Name and information',to:'/players/name'},
{ heading: ‘Grnd', content: ‘Ground Information', to:'/players/grounds' },
{ heading: ‘Infos', content: ‘Match Informations', to:'/players/info' }
]
};
},
methods: {
cardAction(value) {
if(value) {
this.$router.push(value.to);
}
}
}
};
</script>
1st level component (PlayersName) -> 2nd level component cards 1 级组件 (PlayersName) -> 2 级组件卡
<template>
<vu-page background="gray">
<vu-container>
<vu-row-control class="mt-3">
<vu-box-control :list="cardLists" @click="cardAction"></vu-box-control>
</vu-row-control>
</vu-container>
</vu-page>
</template>
<script>
export default {
data() {
return {
cardLists: [
{ heading: ‘Players Personal Info', to:’/players/name/pinfo'},
{ heading: ‘Players Statistics', to:'/players/name/statistics' },
{ heading: ‘Players Records',to:’/players/name/records' }
]
};
},
methods: {
cardAction(value) {
if(value) {
this.$router.push(value.to);
}
}
}
};
</script>
Box-control code snippet盒子控制代码片段
<template>
<div class="box-wrapper">
<div v-for="(items, i) in boxes" :key="'box-row-' + i" class="box-container">
<vu-button-control
v-for="item in items"
:key="item.heading"
plain
class="box"
:to="item.to"
@click="click($event, item)"
>
<vu-row-control>
<vu-column-control cols="10">
<h1 class="label">{{ item.heading }}</h1>
<p class="body_content">{{ item.content }}</p>
</vu-column-control>
<vu-column-control cols="2">
<vu-button-control @click.stop=“addFavouriteClick(item)">
</vu-button-control>
</vu-column-control>
</vu-row-control>
</vu-button-control>
</div>
</div>
</template>
All other routing cards are following exactly same structure just one difference is the passed data .所有其他路由卡都遵循完全相同的结构,唯一不同的是传递的数据。 So, that's why i want one single component and based on $route path i want to change the data part.
所以,这就是为什么我想要一个组件并基于 $route 路径我想更改数据部分。
I am thinking to use switch case approach but not able to do that conditional routing part.我正在考虑使用 switch case 方法,但无法执行条件路由部分。
Sorry, but I'm not sure I understood your problem fully - but I try to give a solution to what I understood:抱歉,但我不确定我是否完全理解您的问题 - 但我尝试为我理解的内容提供解决方案:
To achieve that I'd extend the router
to pass subroutes
props to a general component:为了实现这一点,我将扩展
router
以将subroutes
道具传递给通用组件:
const PlayersInfo = { template: ` <div>Players info component</div> ` } const PlayerStatistics = { template: ` <div>Player statistics component</div> ` } const PlayerRecords = { template: ` <div>Player records component</div> ` } const GroundsInfo = { template: ` <div>Grounds info component</div> ` } const MatchPlayed = { template: ` <div>Match played component</div> ` } const GroundsRecords = { template: ` <div>Grounds records component</div> ` } const MatchInfo = { template: ` <div>Match info component</div> ` } const MatchHistory = { template: ` <div>Match history component</div> ` } const CommunityResources = { template: ` <div>Community resources component</div> ` } // RoutingControl can be called inside itself const RoutingControl = { props: ["subroutes"], template: ` <div> <router-link v-for="subroute in subroutes":key="subroute":to="{ name: subroute }" class="link" > {{ subroute }} </router-link><br /> <router-view /> </div> ` } const routes = [{ name: 'Root', path: '/', redirect: { path: '/players/entry', }, }, { name: 'Players', path: '/players', component: RoutingControl, meta: { topNavigation: true }, redirect: { path: '/players/entry' }, props: (route) => ({ subroutes: getRouteNames("Players") }), children: [{ name: 'PlayersEntryPage', path: 'entry', component: RoutingControl, props: (route) => ({ subroutes: getRouteNames("PlayersEntryPage"), }), children: [{ name: 'PlayersName', path: 'name', component: RoutingControl, props: (route) => ({ subroutes: getRouteNames("PlayersName"), }), children: [{ name: 'Players info', path: 'players-info', component: PlayersInfo, }, { name: 'Player Statistics', path: 'player-statistics', component: PlayerStatistics, }, { name: 'Player records', path: 'player-records', component: PlayerRecords, }], }, { name: 'Grounds', path: 'grounds', component: RoutingControl, props: (route) => ({ subroutes: getRouteNames("Grounds"), }), children: [{ name: 'Grounds info', path: 'grounds-info', component: GroundsInfo, }, { name: 'Match played', path: 'match-played', component: MatchPlayed, }, { name: 'Grounds records', path: 'grounds-records', component: GroundsRecords, }], }, { name: 'MatchInformation', path: 'info', component: RoutingControl, props: (route) => ({ subroutes: getRouteNames("MatchInformation"), }), children: [{ name: 'Match info', path: 'match-info', component: MatchInfo, }, { name: 'match history', path: 'match-history', component: MatchHistory, }, { name: 'community resources', path: 'community-resources', component: CommunityResources, }], } ], }, ] } ] // get subroutes automatically for router (by route name) function getChildrenRoutesNames(arr) { return (routeName) => { let ret = [] arr.forEach(route => { if (route.name === routeName) { ret = route?.children.map(({ name }) => name) } else { if (route?.children?.length) { ret = [...ret, ...getChildrenRoutesNames(route.children)(routeName)] } } }) return ret } } // "fixing" the routes array scope, so it stays when used // inside that array const getRouteNames = getChildrenRoutesNames(routes) const router = new VueRouter({ routes, }) new Vue({ el: "#app", router, })
.link { padding: 0 8px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> <div id="app"> <router-view /> </div>
I hope this is close to what you were looking for:)我希望这接近你想要的:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.