angular - is there an OnActivate method for angular?
问题描述
I currently have an angular page which implements ngOnInit()
.
Currently i am on /home and after clicking the home link it doesn't trigger ngOnInit()
, is there any onActivate()?
ngOnInit() {
this.getPage(1);
}
解决方案
不是一个非常优雅的解决方案,但我解决了它。以下是我想出的代码,以防任何人也需要它。随时评论我如何改进代码。谢谢!
//app-routing.module.ts
{
path: 'home',
component: HomeComponent,
data: {
title: 'Home'
},
runGuardsAndResolvers: 'always',
}
...
@NgModule({
imports: [RouterModule.forRoot(routes, {onSameUrlNavigation: 'reload'})],
exports: [RouterModule]
})
//home.component.ts
export class HomeComponent extends OnActivate implements OnInit {
constructor(public router: Router) {
super(router);
}
initialisePage(): void {
this.getPage(1);
}
}
//OnActivate.ts
export class OnActivate {
navigationSubscription;
constructor(
public router: Router
) {
this.navigationSubscription = this.router.events.subscribe((e: any) => {
if (e instanceof NavigationEnd) {
this.initialisePage();
}
});
}
initialisePage() {
}
ngOnDestroy() {
if (this.navigationSubscription) {
this.navigationSubscription.unsubscribe();
}
}
}
推荐阅读
- sql - SQL - 搜索性能可以取决于列的数量吗?
- node.js - 如何将配置传递给 forRoot
- python - 从 DataFrame 中拆分行值和计数唯一值
- matplotlib - 我怎样才能摆脱这个虚拟的可映射对象并仍然在 Matplotlib 中绘制我的颜色条?
- javascript - 如何在 JSX 中使用样式标签
- angular - 如何正确路由我的 Angular 子页面?
- c++ - 为什么这个类和这个类的对象在这个 IF 语句中是 LOCAL 的?
- flutter - FutureBuilder 没有解析列表的 AsyncDataSnapshot
- python - 使用 Python itk 在图像中心绘制一个球
- haskell - 了解 `-Rghc-timing` 输出