复制
收藏
提问
全网

vue-router] Named Route 'Layout' has a default child route. When navigating to this named route (:to="{name: 'Layout'}"), 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.

俊美巫师
1个月前
全网
文档
学术
百科
知识库
时间不限
自动

理解问题vue-router] Named Route 'Layout' has a default child route. When navigating to this named route (:to="{name: 'Layout'}"), 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] Named Route 'Layout' has a default child route. When navigating to this named route (:to="{name: 'Layout'}"), 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 中,如果一个命名路由有默认子路由,直接导航到这个命名路由时,默认子路由不会被渲染。应该移除这个命名路由的名称,并使用默认子路由的名称来创建命名链接。

🔗 命名路由问题

  • 默认子路由不渲染:当直接导航到一个有默认子路由的命名路由时,子路由不会被渲染。

🛠️ 解决方法

  • 移除命名:移除父路由的命名。
  • 使用子路由命名:使用默认子路由的名称来创建 <router-link>

📝 示例代码

  • 定义路由
const routes = [
  {
    path: '/user/:username',
    name: 'profile',
    component: User
  },
  {
    path: '/layout',
    component: Layout,
    children: [
      {
        path: '',
        component: DefaultChild
      }
    ]
  }
]
  • 使用子路由命名链接
<router-link :to="{ name: 'defaultChild' }">
  Default Child Route
</router-link>
更专业一些
转笔记
专业
vue-router] Named Route 'Layout' has a default child route. When navigating to this named route (:to="{name: 'Layout'}"), 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] Named Route 'Layout' has a default child route. When navigating to this named route (:to="{name: 'Layout'}"), 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 Router 的性能?
Vue Router 与 React Router 有什么不同?
在线客服