【问题标题】:Lazy loaded module route not able to find懒加载模块路由找不到
【发布时间】:2023-03-19 18:11:02
【问题描述】:

我正在尝试将路由重定向到某个路径,而它是空的。但在第一种情况下,它获得了正确的路径,但在尝试再次重定向时抛出 404。

在 Breadcrumb 中,当点击 Parent 时,它会正确重定向,路线将是“/parent/child/child1”,当点击 Child 1 时它再次正常工作,但在点击显示“Child”时出现问题只有“/parent/child”路线,但应该是“/parent/child/child1”路线,不知道我在这里错过了什么。 我试图给它完整的路径(即)但仍然是同样的问题 {path: '', redirectTo: '/parent/child/child1', pathMatch: 'full'},

// App routing module
    export const App_Route: Routes = [
 { path: 'parent',
                data: {pageTitle: 'Parent'},
 loadChildren:'./modules/parent/parent.module#ParentModule'},
]

// Parent module routing

export const Parent_Route: Routes = [
{ path: '', redirectTo: 'child/child1', pathMatch: 'full' },
{
        path: '',
        component: rootWrapper,
        children: [
            {
                path: 'child',
                data: {pageTitle: 'Child Component'},
                loadChildren:'./child/child.module#ChildModule'
            },
]

// Child module routing

export Child_Route: Routes = [
{path: '', redirectTo: 'child1', pathMatch: 'full'},
{
        path: '',
        component: rootWrapper,
        children: [
            {   path: 'child1',
                component: Child1COmponent,
                data: {
                    pageTitle: 'Child 1',
                    authorities: ['ROLE_ADMIN'],
                }]}]

实际:它抛出 404 错误。 预期:它应该重定向到 /parent/child/child1

【问题讨论】:

  • 你能分享一个这个问题的工作例子吗?
  • 我认为您的路线不适合您的用例。如您所知,您的所有子路由都附加到您的根路由,并且 Router 执行深度优先,第一次匹配获胜。这就是第一次点击(在父级上)有效的原因。
  • 当你点击'parent'::: parent route clicked -> 第一个空路径路由(在父模块中)激活 -> 重定向到 child/child1 发出时会发生这种情况 -> 第二个空路径路由(在子路径中)已激活(因为第一个空路径中的路径匹配)-> child1 路径(第二个空路径路径的子路径)已激活。使用这种工作机制,当您单击 child 时,第一个空路径(在子模块中)被激活,导致 child1 重定向,但 child1 不是兄弟路由,因此会发出 404。

标签: angular angular2-routing angular7-router


【解决方案1】:

尝试在您的路由代码中进行以下给定修改:

export const Parent_Route: Routes = [
    { path: '', redirectTo: 'child/child1', pathMatch: 'full' },
    {
            path: 'child',
            component: rootWrapper,
            children: [
                {
                    path: '',
                    data: {pageTitle: 'Child Component'},
                    loadChildren:'./child/child.module#ChildModule'
                },
    ]

// Child module routing

export Child_Route: Routes = [
{path: '', redirectTo: 'child1', pathMatch: 'full'},
{
        path: 'child1',
        component: rootWrapper,
        children: [
            {   path: '',
                component: Child1COmponent,
                data: {
                    pageTitle: 'Child 1',
                    authorities: ['ROLE_ADMIN'],
                }]}]

【讨论】:

  • 还是同样的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
相关资源
最近更新 更多