每当子路由路径为空路径时,将第一个子路由设为活动 class

Make the first child route as the active class whenever the sub-route path is an empty path

我有一个带有一些 link 的主导航导航栏。在主页中,即在根路径 ('/') 中,我嵌套了(子)路由。

路由配置:

const routes = [
    {path: '/', component: HomeMainContent, children: [
            {path: '', component: MovieList},
            {path: 'in-theaters', component: MovieList},
            {path: 'coming-soon', component: ComingSoonList},
            {path: 'trailers', component: TrailerList},
    ]},
    {path: '/about', component: About},
    {path: '/contact',component: Contact},
];

所以当根路径('/')激活时,它的子路由器根路径('')被激活。

子组件路由器模板是这样的:

<template>
    <div id="homeSubMenuWrapper">
        <ul id="subMenuList">
            <li v-for="menu in menuList">
                <router-link class="menuItem" :to="menu.path">{{menu.name}}</router-link>
            </li>
        </ul>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                menuList: [
                    {name: 'IN THEATER', path: '/in-theaters'},
                    {name: 'COMING SOON', path: '/coming-soon'},
                    {name: 'TRAILERS', path: '/trailers'},
                ]
            }
        }
    }
</script>

由于路径 ('') 和 ('in-theaters') 具有相同的组件,我想使路径 ('in-theaters') 的路由器-link 具有router-link-active 的 class 每当其父路径 ('/') 的子路径 ('') 处于活动状态时。我该怎么做?

意味着第一个子路由 ('in-theaters') 应该有活动的 class 只要子路由路径是空路径 ('').

根据@Potray 的建议,我最终检查了模板中的 $route.path 和子路径。

<template>
    <div id="homeSubMenuWrapper">
        <ul id="subMenuList">
            <li v-for="menu in menuList">
                <router-link  :class="[$route.fullPath ==='/' && menu.path === '/in-theaters' ? 'menuItem router-link-active' : 'menuItem']" :to="menu.path">{{menu.name}}</router-link>
            </li>
        </ul>
    </div>
</template>