调用另一个方法时,Angular`subsbe`不设置值

时间:2018-04-30 06:30:17

标签: angular angular5 angular-observable

我订阅数据时有一个Observable,如果我没有设置setTimeout我无法获得该对象的childnode。目前我设置了超时。但寻找正确的角度方法。

这是我的代码:

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: {…}}

1 个答案:

答案 0 :(得分:1)

为值提供任意类型或提供相应的接口,并在订阅功能中提供检查。

what does it say?
should it say more?

希望这有效。