Angular,在页面中放置路由器插座的位置
Angular, where in the page to put router-oulet
我有一个 Angular 8 应用程序,我尝试让延迟加载工作。
用谷歌搜索了很多。
看来一切正常。但不是以正确的方式。因为我有一个页面,并且在该页面上有图标,您将在其中被重定向到具有该 ID 的单独页面。
因此 html 模板如下所示:
<app-topbar header="Hulpbronnen overzicht">
</app-topbar>
<!-- <app-vital10-page header="Hulpbronnen overzicht">
</app-vital10-page> -->
<div class="inner-body">
<app-is-loading *ngIf="!resourcesLoaded" message="Hulpbronnen worden geladen"></app-is-loading>
<app-no-entries
*ngIf="!hasResources && resourcesLoaded"
type="hulpbronnen"
[loads]="resourcesLoaded"
></app-no-entries>
<div class="mobile-resource-filter" (click)="showFilterForMobile = true" *ngIf="allResourceThemesKeys.length > 0">
<span class="fa fa-filter"></span>
</div>
<div class="resources">
<div class="resources-main">
<div class="resource-row" *ngFor="let key of resourceThemesKeys" [@fade]>
<h3 class="resource-row-title">{{ key }}</h3>
<div class="resource-items">
<app-resource-item *ngFor="let item of resourceThemes[key]" [resource]="item">
</app-resource-item>
</div>
</div>
</div>
<div
class="resources-side"
*ngIf="allResourceThemesKeys.length > 0"
[ngClass]="{'stuck-to-top': showFilterForMobile}"
>
<div class="resources-filter resource-row">
<h3 class="resources-header resources-header-filter resource-row-title">Thema Filter</h3>
<div class="resources-filter-body">
<div class="resource-filter-item">
<label for="filter-all" class="resources-filter-label">
<input
type="checkbox"
class="resources-filter-input resources-filter-input-all"
id="filter-all"
(change)="filterAll(allOn)"
[checked]="!allOn"
/>
Filter alles
</label>
<div class="resource-filter-close" *ngIf="showFilterForMobile">
<button type="button" class="button" (click)="showFilterForMobile = false">Sluit</button>
</div>
</div>
<div class="resources-filter-item">
<label for="{{ theme }}" class="resources-filter-label" *ngFor="let theme of allResourceThemesKeys">
<input
type="checkbox"
id="{{ theme }}"
class="resources-filter-input"
[checked]="resourceThemesKeys.indexOf(theme) !== -1"
(change)="handleFilterChange(theme)"
/>
{{ theme }}
</label>
</div>
</div>
</div>
</div>
</div>
</div>
<router-outlet></router-outlet>
路由器模块如下所示:
const ResourceRouters: Routes = [
{
path: '',
component: ResourcePageComponent,
children: [
{path: '', pathMatch: 'full', canActivate: [AuthGuard] },
{path: 'detail/:hulpbronId', component: ResourceDetailComponent, canActivate: [AuthGuard]}
]
}
];
@NgModule({
imports: [
RouterModule.forChild(ResourceRouters)
],
exports: [RouterModule]
})
主要 url 看起来像这样:
http://localhost:4200/hulpbronnen
然后例如您的 ID 为:
http://localhost:4200/hulpbronnen/detail/6688089b-9794-4169-8569-260d427bed03
但是现在那个id的内容会呈现在主页面上,而不是在他自己的组件上。
它必须是什么
在app.routes.ts中我是这样的:
{path: 'hulpbronnen', loadChildren: () => import('./resource/resource.module').then(m => m.ResourceModule)},
所以我的问题is:where我必须把
<router-outlet></router-outlet>
谢谢
这样子页面才能正确显示
如果适合您,您可以尝试以下方法:
创建一个新组件 ResourceIndexComponent
- 将 <router-outlet></router-outlet>
放入那里的 html 模板中。
以这种方式重构 ResourceRoutes:
const ResourceRouters: Routes = [
{
path: '',
component: ResourceIndexComponent,
children: [
{ path: '', pathMatch: 'full', component: ResourcePageComponent, canActivate: [AuthGuard] },
{ path: 'detail/:hulpbronId', component: ResourceDetailComponent, canActivate: [AuthGuard] },
],
},
];
我有一个 Angular 8 应用程序,我尝试让延迟加载工作。
用谷歌搜索了很多。
看来一切正常。但不是以正确的方式。因为我有一个页面,并且在该页面上有图标,您将在其中被重定向到具有该 ID 的单独页面。
因此 html 模板如下所示:
<app-topbar header="Hulpbronnen overzicht">
</app-topbar>
<!-- <app-vital10-page header="Hulpbronnen overzicht">
</app-vital10-page> -->
<div class="inner-body">
<app-is-loading *ngIf="!resourcesLoaded" message="Hulpbronnen worden geladen"></app-is-loading>
<app-no-entries
*ngIf="!hasResources && resourcesLoaded"
type="hulpbronnen"
[loads]="resourcesLoaded"
></app-no-entries>
<div class="mobile-resource-filter" (click)="showFilterForMobile = true" *ngIf="allResourceThemesKeys.length > 0">
<span class="fa fa-filter"></span>
</div>
<div class="resources">
<div class="resources-main">
<div class="resource-row" *ngFor="let key of resourceThemesKeys" [@fade]>
<h3 class="resource-row-title">{{ key }}</h3>
<div class="resource-items">
<app-resource-item *ngFor="let item of resourceThemes[key]" [resource]="item">
</app-resource-item>
</div>
</div>
</div>
<div
class="resources-side"
*ngIf="allResourceThemesKeys.length > 0"
[ngClass]="{'stuck-to-top': showFilterForMobile}"
>
<div class="resources-filter resource-row">
<h3 class="resources-header resources-header-filter resource-row-title">Thema Filter</h3>
<div class="resources-filter-body">
<div class="resource-filter-item">
<label for="filter-all" class="resources-filter-label">
<input
type="checkbox"
class="resources-filter-input resources-filter-input-all"
id="filter-all"
(change)="filterAll(allOn)"
[checked]="!allOn"
/>
Filter alles
</label>
<div class="resource-filter-close" *ngIf="showFilterForMobile">
<button type="button" class="button" (click)="showFilterForMobile = false">Sluit</button>
</div>
</div>
<div class="resources-filter-item">
<label for="{{ theme }}" class="resources-filter-label" *ngFor="let theme of allResourceThemesKeys">
<input
type="checkbox"
id="{{ theme }}"
class="resources-filter-input"
[checked]="resourceThemesKeys.indexOf(theme) !== -1"
(change)="handleFilterChange(theme)"
/>
{{ theme }}
</label>
</div>
</div>
</div>
</div>
</div>
</div>
<router-outlet></router-outlet>
路由器模块如下所示:
const ResourceRouters: Routes = [
{
path: '',
component: ResourcePageComponent,
children: [
{path: '', pathMatch: 'full', canActivate: [AuthGuard] },
{path: 'detail/:hulpbronId', component: ResourceDetailComponent, canActivate: [AuthGuard]}
]
}
];
@NgModule({
imports: [
RouterModule.forChild(ResourceRouters)
],
exports: [RouterModule]
})
主要 url 看起来像这样:
http://localhost:4200/hulpbronnen
然后例如您的 ID 为:
http://localhost:4200/hulpbronnen/detail/6688089b-9794-4169-8569-260d427bed03
但是现在那个id的内容会呈现在主页面上,而不是在他自己的组件上。
它必须是什么
在app.routes.ts中我是这样的:
{path: 'hulpbronnen', loadChildren: () => import('./resource/resource.module').then(m => m.ResourceModule)},
所以我的问题is:where我必须把
<router-outlet></router-outlet>
谢谢
这样子页面才能正确显示
如果适合您,您可以尝试以下方法:
创建一个新组件 ResourceIndexComponent
- 将 <router-outlet></router-outlet>
放入那里的 html 模板中。
以这种方式重构 ResourceRoutes:
const ResourceRouters: Routes = [
{
path: '',
component: ResourceIndexComponent,
children: [
{ path: '', pathMatch: 'full', component: ResourcePageComponent, canActivate: [AuthGuard] },
{ path: 'detail/:hulpbronId', component: ResourceDetailComponent, canActivate: [AuthGuard] },
],
},
];