在我的app.component.html上,如果我在某条路线上,我有条件显示一个组件:
app.component.html
<div *ngIf="router.url === '/home'">
<app-slider></app-slider>
</div>
app.component.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
...
constructor(public router: Router) {}
当页面第一次加载但是当我转到另一条路线然后返回到主路线时,它无法加载,因此我必须重新加载要显示的组件的页面。
我怎样才能解决这个问题?
你可以每次检查激活的路线。
import { ActivatedRoute } from '@angular/router';
constructor(public activatedRoute: ActivatedRoute) {
this.activatedRoute.url.subscribe(res => {
// array of route parts here
});
}