【发布时间】:2017-07-13 16:36:08
【问题描述】:
我是 Angular2 的绝对初学者,
这是我的组件,每当在 FoodDetailsComponent 中触发 buynow() 时,如何更新 HeaderComponent 中的 this.product 数组
export class HeaderComponent {
products: Array < any > ;
cart: Array < any > ;
Tabs: Array < any > ;
constructor(public ShopDataService: ShopDataService) {
this.products = this.ShopDataService.get();
}
ngOnInit() {}
}
export class FoodDetailsComponent {
@Input()
foodDetail: any;
constructor(private ShopDataService: ShopDataService) {
}
buynow(product) {
this.ShopDataService.add(product);
}
ngOnInit() {}
}
而且我对变更检测的工作原理毫无疑问,changeDetectionStrategy 仅适用于子父组件。兄弟组件如何工作?
【问题讨论】:
-
显示你的 html,这些组件是如何相关的。你在哪里使用
onPush -
这是主要的 html,
-
阅读 Everything you need to know about change detection in Angular 了解 onPush 的工作原理
-
尝试这样的公共变更检测:ChangeDetectorRef this.changedetect.detectChanges()
-
这个主html,在路由器出口,shopkartHeader html ,
标签: angular angular2-changedetection