Angular2 APP_INITIALIZER not consistent

前端 未结 4 1901
名媛妹妹
名媛妹妹 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:21

    I think the problem is caused because you subscribe to the observable. This should work

    @Injectable()
    export class UserService {
    
        public user: User;
        constructor(private http: Http) { }
    
        getUser(): Promise {
            console.log('get user called');
            return observable= this.http.get('/auth/getuser', { headers: getHeaders() })
                .map(extractData)
                .do(user => {
                    this.user = user;
                    console.log(this.user)
                 })
                .toPromise();
        }
    }
    

    I'm not sure if toPromise() is necessary. I'd expect it to work with Observable as well.

提交回复
热议问题