Angular 5 Parent在子路由更改时重新加载

时间:2018-05-04 10:51:07

标签: angular5 angular-router

我遇到一个问题,每当其中一个子组件发生更改(子路由更改)时,我的父组件(LoggedInComponent)就会重新加载。

我已经搜索了高低的答案,但似乎无法找到适合我情况的任何内容。

这是我的app-routing.module.ts

const routes: Routes = [
  { path: 'login', component: LoginComponent },
  {
    path: '', component: LoggedInComponent, canActivateChild: [AuthGuard], children: [
      { path: '', redirectTo: 'home', pathMatch: 'full' },
      { path: 'home', component: DashboardComponent },

      { path: 'groups', component: GroupsComponent, data: { role: [PermissionEnum.Groups_View] } },
      { path: 'groups/edit/:id', component: GroupDetailComponent, data: { role: [PermissionEnum.Groups_Edit] } },
      { path: 'groups/create', component: GroupDetailComponent, data: { role: [PermissionEnum.Groups_Create] } },

      { path: 'users', component: UsersComponent, data: { role: [PermissionEnum.Users_View] } },
      { path: 'users/edit/:id', component: UserDetailComponent, data: { role: [PermissionEnum.Users_Edit] } },
      { path: 'users/create', component: UserDetailComponent, data: { role: [PermissionEnum.Users_Create] } },

      { path: 'profile', component: ProfileComponent },
      { path: 'profile/:tabindex', component: ProfileComponent },

      { path: 'settings', component: SettingComponent, data: { role: [PermissionEnum.Global_Settings_View] } },

      { path: 'external-login/:result', component: ExternalLoginProvidersComponent },

      { path: 'permissions/:id/:type', component: PermissionsComponent, data: { role: [PermissionEnum.Users_AssignPermissions] } },
      { path: 'permission-denied', component: PermissionDeniedComponent },

      { path: 'reference-data/:type', component: ReferenceDataComponent, data: { role: [PermissionEnum.Sms_Template_View] } },
      { path: 'reference-data/:type/edit/:id', component: ReferenceDataDetailsComponent, data: { role: [PermissionEnum.Sms_Template_Edit] } },
      { path: 'reference-data/:type/create', component: ReferenceDataDetailsComponent, data: { role: [PermissionEnum.Sms_Template_Create] } },

      { path: 'tenants', component: TenantsComponent, data: { role: [PermissionEnum.Tenant_View] } },
      { path: 'tenants/edit/:id', component: TenantDetailComponent, data: { role: [PermissionEnum.Tenant_Edit] } },
      { path: 'tenants/create', component: TenantDetailComponent, data: { role: [PermissionEnum.Tenant_Create] } },

      { path: 'sms-campaigns', component: SmsCampaignsComponent, data: { role: [PermissionEnum.SmsCampaign_View] } },
      { path: 'sms-campaigns/create', component: CreateSmsCampaignComponent, data: { role: [PermissionEnum.SmsCampaign_Create] } },
      { path: 'sms-campaigns/details/:id', component: SmsCampaignDetailsComponent, data: { role: [PermissionEnum.SmsCampaign_View] } },

      { path: 'document-library', component: LibraryDocumentsComponent },

      { path: 'report-management', component: ReportManagementComponent },
      { path: 'report-management/create', component: CreateReportComponent },
      { path: 'report-management/:id', component: IdpComponent },
      { path: 'report-management/edit/:id', component: ReportDetailsComponent },
      { path: 'report/:reportName', component: ReportComponent }
    ]
  }
];

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

我的app.component.html中有一个主路由器插座,它在登录后会带你到LoggedInComponenet,它有一个页眉,页脚,左侧菜单和另一个孩子们的路由器插座。

这是我的LoggedIn.componenent.html

<app-header></app-header>
<div class="m-grid__item m-grid__item--fluid m-grid m-grid--ver-desktop m-grid--desktop m-body">
  <app-left-menu></app-left-menu>
  <div *ngIf="loading">
    <app-loading-indicator></app-loading-indicator>
  </div>
  <div class="center-display" *ngIf="childrenLoadingAllowed">
    <router-outlet class="m-grid__item m-grid__item--fluid m-wrapper" [ngClass]="{ hidden: loading }"></router-outlet>
  </div>
</div>
<app-footer></app-footer>

然后我有LoggedIn.component.ts

import { Component, OnInit } from '@angular/core';
import { BaseComponent } from '../shared/base.component';

@Component({
  selector: 'app-logged-in',
  templateUrl: './logged-in.component.html',
  styleUrls: ['./logged-in.component.css']
})
export class LoggedInComponent extends BaseComponent implements OnInit {

  public loading = true;
  public childrenLoadingAllowed = false;

  constructor() {
    super();
  }

  ngOnInit() {
    this.layoutService.setLoadingEvent
      .subscribe((res: boolean) => {
        if (this.loading !== res)
          this.loading = res;
      });
  }
}

然后最后这里是左侧菜单,当我加载孩子

时不断重新加载
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { BaseComponent } from '../../shared/base.component';
import { PermissionEnum, LookupClient, LookupType, LookUpDto } from '../../../services/web-api-generated';

@Component({
  selector: 'app-left-menu',
  templateUrl: './left-menu.component.html',
  styleUrls: ['./left-menu.component.scss'],
  encapsulation: ViewEncapsulation.None
})

export class LeftMenuComponent extends BaseComponent implements OnInit {

  public hasReports = false;

  public reports: Array<LookUpDto> = new Array<LookUpDto>();

  constructor(private lookupClient: LookupClient) {
    super();

    this.loadReportMenuItems();
  }

  ngOnInit() {
    this.layoutService.rebuildReportMenu
      .subscribe(res => {
        this.loadReportMenuItems();
      });
  }

  private loadReportMenuItems(): void {
    this.lookupClient.getLookUpValues(LookupType.MunicipalReports)
      .subscribe((res: Array<LookUpDto>) => {
        this.reports = res;

        this.reports.forEach(element => {
          element.value = element.value.replace(/\s+/g, '-').toLocaleLowerCase();
        });

        this.hasReports = res.length > 0;
      });
  }
}

3 个答案:

答案 0 :(得分:0)

我通过将api调用移动到带有变量的服务来解决问题,如果数据尚未设置或者强制变量通过则只加载数据。

答案 1 :(得分:0)

我相信这是这里提到的错误:https://github.com/angular/angular/issues/18374

答案 2 :(得分:-1)

是的,canActivateChild在子路径之间进行更改时会重新加载整个父组件