【发布时间】:2019-01-13 14:43:25
【问题描述】:
我的代码似乎并没有等待执行某些代码并走得更远。
isAdmin(): boolean {
this.getLoggedUser();
this.getRole();
console.log(this.admin);
console.log('isAdmin');
return this.admin;
}
getRole() {
this.httpClient.get<boolean>('/api/has-role-admin').
subscribe((data) => {
this.admin = data;
console.log(this.admin);
console.log('getRole');
}
);
}
控制台结果:
false
isAdmin
true
getRole
我想完成 getRole() 方法,然后在 isAdmin() 中完成该步骤。我怎么会收到这种行为?
【问题讨论】:
-
不要
subscribe。像这样的东西:stackoverflow.com/q/49444816/1132334 -
另外,请记住
console.log在某些浏览器中是异步的。 -
感谢您的建议,这次 console.log 是正确的 :) 但是另一个问题是当我将 isAdmin() 创建为异步时,如何让它仍然返回布尔值?
标签: angular typescript