大家好! 谁能告诉我如何在每次页面加载后调用 Angular 7 函数?
最佳答案
您可以在加载的第一个页面(通常是app.components.ts
)中订阅路由器事件
,并检查该事件的实例是否为导航结束
。
为此,您必须这样做:
import { Router,NavigationEnd } from '@angular/router';
constructor(
private router: Router
) {
this.router.events.subscribe((e) => {
if (e instanceof NavigationEnd) {
// Function you want to call here
}
});
}
这里有一个实例:
https://stackblitz.com/edit/angular-routing-function-stack-55723837?file=src/app/app.component.ts
关于angular - 如何在每次加载页面后调用 Angular 7 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55723837/