angular - Angular routerLink 不会触发 ngOnInit
问题描述
这是 componet.ts 文件中的 ngOnInit
ngOnInit() {
this.locationService.getLocation().subscribe( locations => {
this.locations = locations;
});
}
<a [routerLink]="['/locations-list']">
当我使用 a[routerLink]
导航到上述组件时,它会导航到该组件并加载视图,但它不会在ngOnInit方法之上触发。但是,如果我刷新页面,它就可以正常工作。
有没有解决上述问题的方法。
我已经习惯href
了导航到页面,并且使用href
上述方法总是可以正常工作,但速度很慢。这就是为什么我更改href
为[routerLink]
.
这是包含视图的 component.html
<div class="table-responsive">
<table class="table">
<thead class=" text-primary">
<th>
Location Name
</th>
</thead>
<tbody *ngIf="locations?.length > 0">
<tr *ngFor="let location of locations">
<td *ngIf="location.verified != false">
{{location.locationName}}
</td>
</tr>
</tbody>
</table>
</div>
</div>
解决方案
当路由器参数更改时,基本路由保持不变。所以它不会触发ngOnInit
. 所以订阅路由事件
ngOnInit() {
this.route.params.subscribe(
params => {
// your code
});
}
推荐阅读
- javascript - Typescript / Angular中textarea元素内的选项卡
- java - 测试容器在 Spring Boot 项目中启动两个容器而不是一个
- amazon-web-services - 如何为远程端口转发配置 AWS EC2 实例?
- google-docs - 如何从 Google Docs 的表格中删除单元格
- powershell - 使用powershell根据文件名按顺序移动文件
- html - ckeditor 使页面无响应
- acumos - 无法在本地 VM 上部署 Acumos 门户。|
- php - 如何在 Codeigniter 中使用 update_batch 使用算术运算符更新我的表?
- python - 多个文本文件中特定单词的计数
- css - 当子组件在其顶部呈现时如何禁用父组件上的单击/滚动