首页 > 解决方案 > 每次加载页面后如何调用 Angular 7 函数

问题描述

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

标签: angular

解决方案


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


推荐阅读