У меня есть Angular 7-компонент страницы, который содержит другой компонент. При выборе продукта из поля выбора на маленьком компоненте он переходит на другую страницу с тем же компонентом страницы (ProductDetails), но с другими параметрами в URL (другой идентификатор продукта).
Навигация по маленькому компоненту при выборе товара:
onProductSelected(args) {
const selectedOption = args.target.value;
this.router.navigate(['/products', selectedOption]);
}
page-component ProductDetails.ts:
@Component({
selector: 'product-details',
templateUrl: './product-details.component.html',
styleUrls: ['./product-details.component.scss']
})
export class ProductDetailsComponent implements OnInit, OnDestroy {
currentProductId: string;
product: ProductModel;
subscriber;
constructor(private productService: ProductService,
private route: ActivatedRoute) {}
ngOnInit() {
this.currentProductId = _.get(this.route, 'snapshot.params.productId');
this.subscriber = this.productService.getProdactById(this.currentProductId)
.subscribe(res => {
this.product = res;
});
}
ngOnDestroy() {
if (this.subscriber) {
this.subscriber.unsubscribe();
}
}
}
Мне нужно ngOnInit
компонента страницы, чтобы заметить, что URL изменился, и пересоздать себя. (чтобы получить новый параметр productId и отобразить детали продукта).
Как я могу это сделать?
Спасибо!