Angular2 APP_INITIALIZER not consistent

前端 未结 4 1910
名媛妹妹
名媛妹妹 2020-12-06 01:57

I am using APP_INITIALIZER like it is recommended in this answer, with my service returning a promise, but it doesn\'t always wait for it to resolve and I can see my compone

4条回答
  •  生来不讨喜
    2020-12-06 02:22

    Try the following code:

    getUser(): Promise {
        console.log('get user called');
        var promise = this.http.get('/auth/getuser', {headers: getHeaders()})
            .map(extractData)
            .toPromise();
        promise.then(user => {
            this.user = user;
            console.log(this.user);
        });
        return promise;
    }
    

    I was facing the same problem, and using a promise instead of a observable did the trick for me.

提交回复
热议问题