在我的项目中,我有一个菜单,它在*ngFor循环中,我将menu.Id作为参数从数据库中加载数据
<li *ngFor="let item of menu">
<a [routerLink]="['/products', item .Id]">{{item .Name}}</a>
</li>
当我第一次点击这个菜单项时(不管是哪一个),它工作得很好,但之后再点击另一个菜单项,它就不能工作了,因为ProductsComponent
已经加载了,不能再接受Id (路由没有改变,'/products/: Id‘Id不同,但构造函数已经加载,组件已经初始化)。
在ProductsComponent
的构造函数中,像这样获取数据
this.route.params.subscribe(params => {
this.menuId = params.menuId;
});
发布于 2018-06-05 13:22:35
您只需在加载时从路径获取所需的信息
例如,在ngOnInit中,可以使用以下函数调用getMenuId():
getMenuId() {
const id = +this.route.snapshot.paramMap.get('menuId');
//do something with menuId, like call another function to load your data with the id
}
https://stackoverflow.com/questions/50687374
复制相似问题