【问题标题】:Angular routing - routing to a lazy module child path does not initiate the component角度路由 - 路由到惰性模块子路径不会启动组件
【发布时间】:2019-05-08 03:56:59
【问题描述】:

在我的 app-routing.module 中有一个惰性模块:

const appRoutes: Routes = [
    { path: 'profile/:id',
        loadChildren: './profile/profile-standalone/profile-standalone.module#ProfileStandaloneModule',
    }
];

这是我的个人资料--standalone-routing.module-

const routes: Routes = [
    {
        path: '',
        component: ProfileStandaloneComponent,
        children: [
            {
                path: '',
                redirectTo: 'outputa',
                pathMatch: 'full'
            },
            {
                path: 'outputa',
                component: ProfileOutputComponent
            }
        ]
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})
export class ProfileStandaloneRoutingModule { }

但是当我转到 url "localhost:4200/profile/123/outputa" 时,我的 "ProfileOutputComponent" 没有被调用。

知道如何让它工作吗?

【问题讨论】:

  • 由于两个路由器都重定向到相同的状态,因此 Angular 将采用最先收到的第一条路由,您甚至将路径匹配“完整”,这也将满足第一个状态。尝试更改您的路线结构。

标签: javascript angular typescript routing lazy-loading


【解决方案1】:

试试这个。

const routes: Routes = [
{
    path: 'outputa',
    component: ProfileOutputComponent
];

【讨论】:

  • 哦,我们不能为父路由path = '' 放置孩子,因为对于那些没有任何名字的父母,我们不能有child route。因此,最好为路径提供一些名称,然后只有上述解决方案才能工作。
  • 但即使我删除“路径:”并只留下“输出”,它也不起作用
  • 这对你有用吗'localhost:4200/profile/123/'?
  • 这是您的文件夹名称“/profile/profile-standalone/”吗?尝试最新的编辑。
  • 是的,功能模块在 profile/profile-standalone 中。 ProfileOutputComponent 位于不同的目录中。
猜你喜欢
  • 2018-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-11
  • 2021-08-26
  • 2019-03-28
  • 2018-11-30
  • 2021-03-26
相关资源
最近更新 更多