angular - Angular 在服务器响应之前停止程序的执行
问题描述
问题:有一个可以为空的布尔属性isAuthorized,在服务构造函数中向服务器发送请求以获取有关用户的信息并确保他被授权。
private _isAuthorized: boolean | null = null;
public get isAuthorized() {
return this._isAuthorized;
}
constructor(private http: HttpClient) {
this.update();
}
async update() {
try {
this.data = await this.getUserInfo();
console.log(this.data.userName);
this.userName = this.data.userName;
this._isAuthorized = true;
}
catch (err) {
...
}
}
getUserInfo(){
...
return this.http.get<User>(this.url, { headers: header }).toPromise();
}
但是,可以在 isAuthorized 属性仍然为空时访问它(即,直到服务器响应为止)。
解决方案
推荐阅读
- xslt-1.0 - 我想为每个循环增加一个值。假设表 1、2、3、4、5、6、7、8 中的 No. 列共有 8 个循环值增量
- data-structures - 为什么三重链表不存在?
- php - 无法从 StudentController 更新功能更新学生数据
- kubernetes - 为什么 helm 忽略节点端口部分?
- regex - Ace 编辑器中的多行正则表达式搜索和替换
- visual-studio-code - Vscode intelliSense 不适用于 wx 模块
- c - 字每 2 位符号
- json - 在 Spark 中解析类似 Json 的结构
- php - 不懂 SQL 参数
- java - 无法执行显示“过时元素引用:元素未附加到页面文档”的代码