Vue-Router命名路由存在默认子路由的问题


Named Route 'index' has a default child route. When navigating to this named route (:to="{name: 'index'}"),使用Vue-Router进行路由配置时,如果命名路由存在默认子路由,则当导航到该命名路由时,会出现默认子路由不会被渲染的问题。解决方法为移除父级命名路由的name属性,将命名路由指向默认子路由的name属性即可。本文对该问题进行了详细讲解。

报错信息:

Named Route 'index' has a default child route. When navigating to this named route (:to="{name: 'index'}"), the default child route will not be rendered. Remove the name from this route and use the name of the default child route for named links instead.

Vue-Router命名路由存在默认子路由的问题

在Vue的项目中使用了Vue-Router,当某个路由有子级路由时,如下写法

const routes = [
    {
        path: "/index",
        name: "index",
        component: () => import("@/views/Layout.vue"),
        children: [
            {
                path: "",
                name: "indexcontent",
                component: () => import("@/views/IndexContent.vue"),
            },
        ],
    },
];

按照以上写法,就会报出如下警告:

Vue-Router命名路由存在默认子路由的问题

解决办法:

只需将父级的name属性去除即可解决这个问题,因为当某个路由有子级路由的时候,这时候父级路由就需要一个默认的路由,所以父级路由不能定义name属性

const routes = [
    {
        path: "/index",
        component: () => import("@/views/Layout.vue"),
        children: [
            {
                path: "",
                name: "indexcontent",
                component: () => import("@/views/IndexContent.vue"),
            },
        ],
    },
];


扫描二维码,在手机上阅读

推荐阅读:

Vue组件通信详解

Vue组件生命周期详解:从创建到销毁的全过程

评 论