angular - 日志声明变量内容角度7
问题描述
我有一个名为 currentUser 的声明变量,其中包含一个来自 http get 请求的用户对象,我只想记录 currentUser 变量,但我没有定义!有人可以解释一下为什么!
import { Component, OnInit, NgModule } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms';
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
currentUser: any;
user: FormGroup;
cities: any[];
fileToUpload: any;
avatarPath: any;
avatarURL: any;
returnUrl: any;
constructor(
private http: HttpClient,
private fb:FormBuilder
) {
this.createForm();
}
ngOnInit() {
this.http.get('api/user').subscribe(user => {
this.currentUser = user;
localStorage.setItem('currentUser', JSON.stringify(this.currentUser));
});
console.log('this.currentuser :', this.currentUser)
this.http.get('api/cities').subscribe((cities: any[]) => {
this.cities = cities;
});
this.user.patchValue({
firstName: this.currentUser.firstName,
lastName: this.currentUser.lastName,
email: this.currentUser.email,
profession: this.currentUser.profession,
gender: this.currentUser.gender,
birthDate: this.currentUser.birthDate,
phone: this.currentUser.phone,
oldPassword: this.currentUser.oldPassword,
password: this.currentUser.newPassword,
address: this.currentUser.address,
postalCode: this.currentUser.postalCode,
city: this.currentUser.city._id
});
}
解决方案
我认为这是因为同步而发生的。尝试这个:
this.http.get('api/user').subscribe(user => {
this.currentUser = user;
console.log('this.currentuser :', this.currentUser);
localStorage.setItem('currentUser', JSON.stringify(this.currentUser));
});
推荐阅读
- python - 我正在尝试使用 PySimpleGUI 构建一个 python GUI 数据库比较器我在这里遗漏了什么?
- postgresql - 使用 sql 语言的 PostgreSQL 过程中的 IF 语法错误
- laravel - Laravel - 1215 无法添加外部约束
- c# - CarouselView 中的视频问题
- wordpress - WordPress 如何在管理帖子/编辑侧栏上添加另一个带有自定义元框的选项卡?
- javascript - onClick 处理程序以更改元素的颜色
- apache-spark - 如何在 spark rdd 中读取 zip 文件中的分隔文件
- javascript - 无法使用 useState 挂钩设置数组
- r - 使用带有嵌套 GBM 的 PDP 和 map 函数
- node.js - 我怎样才能让lottie动画与nodejs一起工作