我在一个文件中有一个 javascript 函数,我路由到 Angular 项目中的 ng-view。路由完成后有没有办法执行上述功能?类似于 JavaScript 的 window.onload?
最佳答案
是的,你可以使用路由器事件, 在你的构造函数中,
constructor(private router: Router) {
this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
// call your function here
}
});
}
不要忘记导入
import { Router } from '@angular/router';
只有在导航即将结束时才会调用此事件。
**The sequence of router events is as follows,**
NavigationStart,
RouteConfigLoadStart,
RouteConfigLoadEnd,
RoutesRecognized,
GuardsCheckStart,
ChildActivationStart,
ActivationStart,
GuardsCheckEnd,
ResolveStart,
ResolveEnd,
ActivationEnd
ChildActivationEnd
NavigationEnd,
NavigationCancel,
NavigationError
Scroll
这是您可以查看 Angular 文档的所有导航事件 https://angular.io/api/router/Event
关于javascript - 在 Angular 路由更改后调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59298963/