【发布时间】:2020-05-30 02:00:17
【问题描述】:
在我的 Angular 项目中,一个页面包含 2 个名为“head”和“product”的组件。
在“head”中,我可以更改页面的货币。我将货币信息保存在localStorage中。当货币更改时,我通过“next”推送一个随机数,如下所示:
this.service.refreshNumberOfAllPriceByCurrency.next(Math.random()); //"refreshNumberOfAllPriceByCurrency" is defined as ReplaySubject<any>(1) in service component.
该行在“产品”组件中触发订阅,如下所示:
constructor(.........)
{
this.service.refreshNumberOfAllPriceByCurrency.subscribe(number => {
this.gridProduct.instance.refresh();
this.gridPrice.instance.refresh();
});
}
在订阅范围内调试时,“this”上下文不包含 gridProduct、gridPrice 和 linensOfferId。所以无法获取“undefined”的实例……
如何在订阅范围内获取这些网格的实例?
提前致谢……
【问题讨论】:
-
您是否尝试过在 ngOnInit 或 ngAfterViewInit 中订阅?
-
你能在 stackblitz 上重现这个吗?
-
嗨 Wctiger.. 我试过 ngAfterViewInit。没有任何改变。
-
请说明您如何实例化 gridProduct 和 gridPrice。如果您收到它们未定义的错误,那么它们并没有在您希望它们被实例化时被实例化。
-
我正在像这样实例化网格:@ViewChild("gridProduct", { static: false }) gridProduct: any; @ViewChild("gridPrice", { static: false }) gridPrice: any;
标签: angular observable instance subscribe