【问题标题】:how to use changeDetection onPush with observable如何使用可观察的 changeDetection onPush
【发布时间】: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,
  • 尝试这样的公共变更检测:ChangeDetectorRef this.changedetect.detectChanges()
  • 这个主html,
    shopkartHeader html ,
    在路由器出口,

标签: angular angular2-changedetection


【解决方案1】:

获取数据时需要订阅服务。

this.ShopDataService.get().subscribe( result => {this.products = result});

也请在您的服务中分享代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 2018-08-29
    • 2020-08-30
    相关资源
    最近更新 更多