Angular `subscribe` 在调用另一个方法时没有设置值

Angular `subscribe` not setting the value when call another method

我有一个订阅数据的 Observable,如果我没有设置 setTimeout 我无法获得对象的 childnode。目前我设置超时。但正在寻找 angular.

的正确方法

这是我的代码:

ngOnInit() {
        this.store.appData.subscribe(value => {
            this.appData = value;
            setTimeout(() => this.setLocales(), 1000); //requires this
        } );
    }


    setLocales(){
        console.log( this.appData.hdr.country );
    }

我得到的错误:

ERROR TypeError: Cannot read property 'country' of undefined
    at HeaderComponent.setLocales (header.component.ts:32)
    at SafeSubscriber.eval [as _next] (header.component.ts:26)
    at SafeSubscriber.__tryOrSetError (Subscriber.js:252)
    at SafeSubscriber.next (Subscriber.js:192)
    at Subscriber._next (Subscriber.js:131)
    at Subscriber.next (Subscriber.js:95)
    at BehaviorSubject._subscribe (BehaviorSubject.js:30)
    at BehaviorSubject.Observable._trySubscribe (Observable.js:172)
    at BehaviorSubject.Subject._trySubscribe (Subject.js:98)
    at BehaviorSubject.Observable.subscribe (Observable.js:160)

ERROR CONTEXT DebugContext_ {view: {…}, nodeIndex: 17, nodeDef: {…}, elDef: {…}, elView: {…}}

为值提供类型any或提供相应的接口并在订阅函数中提供检查。

ngOnInit() {
    this.store.appData.subscribe((value: any) => {
        if(value && value.hdr) {
        this.appData = value;
        this.setLocales(); 
      }
    } );
}

希望这有效。