angular - 每次加载页面后如何调用 Angular 7 函数
问题描述
大家好!谁能告诉我如何在每次页面加载后调用 angular 7 函数?
解决方案
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
推荐阅读
- oracle - 带有用户创建记录的关联数组的 PLS-00306 错误
- python - PySide2 QListView QTableView 同步问题
- python - python - 如何在出现密钥错误时在python中连接redis服务器?
- http - 如何取消在 Flutter 中使用 http.MultipartRequest() 发送的正在进行的文件上传?
- css - 我可以在 cpanel 中使用 !important 吗?我使用 viewen 并获得了 cpanel 但不能在我的 css 中使用 !important 属性
- reactjs - 在 React+Redux 项目中使用 Bootstrap 和 jQuery 库
- python - 如何使由线程更新的字符串反映 Python 诅咒的变化?
- vue.js - 即使响应为 200(成功),也会调用 Axios Catch
- java - appium`HasClipboard()`的“方法尚未实现”异常
- android - 颤振:断言失败:'builder!= null home!= null