首页 > 解决方案 > Vue嵌套路由不显示其组件且没有错误

问题描述

当我访问 URLprofile/admin时,我会看到配置文件组件,但是,如果我尝试 URLprofile/admin/locationsprofile/admin/map,我不会加载 Map 或 Locations 组件。任何想法为什么?

有问题的路线:

import Home from './components/Home.vue';
import Profile from './components/Profile.vue';
import Map from './components/Map.vue';
import Locations from './components/Locations.vue'

export const routes = [
    { path: '', component: Home, name: 'Home'},
    { path: '/profile/:username', component: Profile, name: 'Profile', chilren: [
        { path: '/profile/:username/locations', component: Locations, name: 'Locations'},
        { path: '/profile/:username/map', component: Map, name: 'Map'}
    ]},
];

配置文件组件模板:

<template>
    <div class="wrapper">
        <p>Profile</p>
        <router-view></router-view>
    </div>
</template>

位置组件模板:

<template>
    <div class="wrapper">
        <p>Location</p>
    </div>
</template>

地图组件模板:

<template>
    <div class="wrapper">
        <p>Location</p>
    </div>
</template>

标签: javascriptvue.jsvuejs2vue-componentvue-router

解决方案


您的路线中有错字 -chilren而不是children.

您不需要在子路由中指定完整路径。正确的做法:

export const routes = [
    { path: '', component: Home, name: 'Home'},
    { path: '/profile/:username', component: Profile, name: 'Profile', children: [
        { path: 'locations', component: Locations, name: 'Locations'},
        { path: 'map', component: Map, name: 'Map'}
    ]},
];

推荐阅读