【发布时间】:2020-07-12 06:34:21
【问题描述】:
我的应用组件 HTML 上有这段代码
<mat-progress-spinner *ngIf="loading"></mat-progress-spinner>
<router-outlet></router-outlet>
还有 TS
export class AppComponent {
loading: boolean;
constructor(private router: Router) {
this.router.events.subscribe((routerEvent: Event) => {
this.checkRouterEvent(routerEvent)
})
}
checkRouterEvent(routerEvent: Event): void {
if (routerEvent instanceof NavigationStart) {
this.loading = true;
console.log(this.loading)
}
if (routerEvent instanceof NavigationCancel ||
routerEvent instanceof NavigationEnd ||
routerEvent instanceof NavigationError) {
this.loading = false
console.log(this.loading)
}
}
}
我通过 Deborah Kurata 的 video 学习了这项技术。但是我从来没有看到微调器,加载页面时我的应用程序中有很多图像。我究竟做错了什么?有什么想法吗?
编辑:
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
使用 onPush 更改检测,图像不会出现,所以我有点迷失在这里......
编辑:
应用路由:
//const adminModule = ()=> import('./admin/admin.module').then(m=>m.AdminModule);
const routes: Routes = [
{
path: '', component:ShellComponent,
children: [
{path: 'home', component: HomeComponent, canActivate:[AuthGuardService] },
//{path: 'admin', loadChildren: adminModule},
{path: 'detail/:id', component: DetailComponent, },
{path: '', redirectTo:'home', pathMatch:'full'},
{path: 'table', component:ListBooksComponent},
{path: 'selection', component: SelectionComponent, },
{path:'edit/:id', component:RegisterComponent, canDeactivate:[EditGuardService]},
]
},
{path: 'login', component: LoginComponent},
{path:'edit/:id', component:RegisterComponent},
{path:'register', component: RegisterComponent}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
【问题讨论】:
-
首先图片的加载不会停止导航,也不是导航生命周期的一部分。您是否有机会在您的应用组件中使用
ChangeDetectionStrategy.OnPush? -
不,我使用默认的,但我可以试试你的建议