Vue嵌套路由不显示其组件且没有错误

时间:2019-01-19 07:58:43

标签: javascript vue.js vuejs2 vue-component vue-router

当我访问URL profile/admin时,会看到配置文件组件,但是,如果尝试URL profile/admin/locationsprofile/admin/map,则不会加载“地图”或“位置”组件。有什么想法吗?

有关路线:

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>

1 个答案:

答案 0 :(得分:3)

您的路线中有错别字-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'}
    ]},
];