每个加载页面后如何调用angular 7函数

时间:2019-04-17 09:05:54

标签: angular

大家好! 谁能告诉我如何在每次加载页面后调用angular 7函数吗?

3 个答案:

答案 0 :(得分:1)

您可以在应用程序组件的ngOnInit()方法中做到这一点。

答案 1 :(得分:1)

您可以在加载的第一页(通常为router events)中订阅app.components.ts,并检查事件的实例是否为NavigationEnd。 为此,您必须执行以下操作:

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

答案 2 :(得分:0)

或订阅路由器NavigationEnd事件。参见https://angular.io/api/router/Event