我有一个组件,可以在其中获取路由参数:
export class CatalogComponent implements OnInit {
category: number;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.paramMap.subscribe(parameters => {
this.category = parameters.has('category') ? +parameters.get('category') : undefined;
})
}
}
然后在我的模板上:
<product-list [category]="category"></product-list>
ProductList 组件是:
export class ProductListComponent implements OnInit {
@Input() category: number;
products$: Observable<ProductListModel[]>;
constructor(private productService: ProductService) { }
ngOnInit() {
this.products$ = this.getProducts();
}
private getProducts(): Observable<ProductListModel[]> {
return this.productService.get(this.category);
}
}
问题是当路由参数category
更改时,ProductList 组件呈现的产品没有更新。
我该如何解决这个问题?