加载同一模块的多路径-延迟加载

时间:2019-07-18 08:16:44

标签: javascript angular typescript

我在一个模块中有多个组件。我想显示基于路由路径的组件。  我想显示http://localhost:4200/account的帐户组成部分。 对于http://localhost:4200/setting,我想显示设置组件..etc

app.routing.module.ts

{
    path: 'account',
    loadChildren: './modules/settings/settings.module#SettingsModule',
},
{
    path: 'settings',
    loadChildren:'./modules/settings/settings.module#SettingsModule', 
},

settings.routing.module.ts

export const routes: Routes = [
    {
        path: 'account',
        component: accountComponent
    },
    {
        path: 'account/edit',
        component: accountEditComponent
    },
    {
        path: 'settings',
        component: settingsComponent
    },
    {
        path: 'settings/edit',
        component: settingsEditComponent
    }
];

我在settings.routing.module.ts中做了哪些更改以显示那些组件。

4 个答案:

答案 0 :(得分:1)

如果您确实要执行此操作,则可以使用UrlMatcher查找正确的组件。

侧面注意:我不建议您这样做。取而代之的是我的其他答案。我认为这是一种更好的方法。当然,这是您的决定。

Simple demo

app.routing.module.ts (未更改)

{
    path: 'settings/account',
    loadChildren: './modules/settings/settings.module#SettingsModule',
},
{
    path: 'settings',
    loadChildren:'./modules/settings/settings.module#SettingsModule', 
}

settings.routing.module.ts

export function isAccount(url: UrlSegment[], group: UrlSegmentGroup) {
  return group.segments.length === 1 && group.segments[0].path.endsWith('account') ? ({consumed: url}) : null;
}

export function isSettings(url: UrlSegment[], group: UrlSegmentGroup) {
  return group.segments.length === 1 && group.segments[0].path.endsWith('settings') ? ({consumed: url}) : null;
}

export const routes: Routes = [
    {
        path: 'account',
        component: accountComponent,
        matcher: isAccount
    },
    {
        path: 'account/edit',
        component: accountEditComponent
    },
    {
        path: 'settings',
        component: settingsComponent,
        matcher: isSettings
    },
    {
        path: 'settings/edit',
        component: settingsEditComponent
    }
];

结果正是您要寻找的:

http://localhost:4200/settings将显示设置组件。

http://localhost:4200/account将显示帐户组成部分。

答案 1 :(得分:0)

一种方法是将settings作为此模块的默认路径(组件),并将所有其他组件作为子路由。

Simple DEMO

app.routing.module.ts

{
    path: 'settings/account',
    loadChildren: './modules/settings/settings.module#SettingsModule',
},
{
    path: 'settings',
    loadChildren:'./modules/settings/settings.module#SettingsModule', 
},

settings.routing.module.ts

export const routes: Routes = [
    {
        path: '',
        component: settingsComponent
    },
    {
        path: 'edit',
        component: settingsEditComponent
    },
    {
        path: 'account',
        component: accountComponent
    },
    {
        path: 'account/edit',
        component: accountEditComponent
    }
];

http://localhost:4200/setting将显示设置组件。

http://localhost:4200/settings/account将显示帐户组成部分。

.. etc

答案 2 :(得分:0)

您可以做这样的事情

settings-routing.module.ts

const routes: Routes = [
    {
        path: '',
        component: PubliclistsComponent,
        children: [
          {path: '', redirectTo: 'all', pathMatch: 'full'},
          {path: 'all', component: ChallengelistComponent},
          {path: 'me', component: ChallengelistComponent}
        ]
    }
];

const settingsRoutes: Routes = [
    {
        path: '',
        component: SettingsComponent,
        children: [
          {path: '', redirectTo: 'participants', pathMatch: 'full'},
          {path: 'bleh', component: TeamlistComponent},
          {path: 'bleh-bleh', component: TeamlistComponent}
        ]
    },
];

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


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

settings.module.ts

@NgModule({
  declarations: [],
  imports: [
    ...
  ]
})
export class AccountModule { }


@NgModule({
  declarations: [],
  imports: [
    ...
  ]
})
export class SettingsModule { }

app-routing.ts

{
    path: 'account',
    loadChildren: './modules/settings/settings.module#AccountsModule',
},
{
    path: 'settings',
    loadChildren:'./modules/settings/settings.module#SettingsModule', 
},

答案 3 :(得分:0)

添加到上面的 benshabatnoam's Answer,您可以在您的模块中创建一个通用匹配器函数

// load components from root path
export function isPath(url: UrlSegment[], group: UrlSegmentGroup, route: Route) {
  let r = '';
  for (const p of group.segments) {
    if (p !== group.segments[0]) {
      r += '/';
    }
    r += p.path;
  }
  return r === route.data!.path
    ? ({ consumed: url })
    : null;
}

const routes: Routes = [
  { component: SettingsComponent, matcher: isPath, data: { 'path': 'settings' } },
  { component: AccountComponent, matcher: isPath, data: { 'path': 'account' } },
{ component: AccountComponent, matcher: isPath, data: { 'path': 'full/path' } }
];