【问题标题】:How to console an observable data in container component如何控制容器组件中的可观察数据
【发布时间】:2020-02-12 23:38:51
【问题描述】:

在我的组件中,我从商店获取数据,如下所示:

ngOnInit() {

        this.appUserName = this.store.pipe(select(subscribe.getAppUserName));
        this.store.dispatch(new actions.LoadTranslationIds());
        this.data = this.store.pipe(select(subscribe.getTranslationIds));

    }

分配后,我试图在“ngChanges”中打印“this.data”,例如:

ngOnChanges() {
     console.log('trans id', this.data);
}

但是ngOnChanges 本身没有触发。我无法控制我收到的数据。我的代码有什么问题?或者正确的方法是什么?

我在这里没有使用“订阅”方法。

【问题讨论】:

    标签: angular8 angular-observable ngrx-store-8.0


    【解决方案1】:

    试试这样:

    this.data = this.store.pipe(select(subscribe.getTranslationIds)).pipe(tap((val) => console.log(val)));
    

    它会为你工作!

    【讨论】:

      【解决方案2】:

      ngOnChanges 仅在指令的任何数据绑定属性发生更改时调用。

      你为什么不订阅它。

      https://angular.io/api/core/OnChanges

      【讨论】:

      • 那是为了获取@input作为数据,我正在寻找容器/智能组件解决方案
      猜你喜欢
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      相关资源
      最近更新 更多