我正在Angular 8中工作。我有一个名为Basic
的模型类,用于保存配置文件的基本信息。
在我的组件中,将从Basic
返回Service
类型的对象,服务将从服务器获取值。在我的组件中,当我打印Basic
的对象时,它显示有值,但是当我尝试访问这些值时,它显示未定义。我的Basic
类有getter / setter。
服务代码
bas: Basic = new Basic();
public getCurrentUserBasic() {
return this.http.get<Basic>(
'http://127.0.0.1:8080/public/profile/basic/findOneByUser', this.authService.getHeader(),
).subscribe((b: Basic) => {
this.loading = false;
console.log(b);
// phone number
for (const key in b['phone_number']) {
const phoneNumber = new PhoneNumber();
phoneNumber.$id = b['phone_number'][key]['id'];
phoneNumber.$number = b['phone_number'][key]['number'];
this.phoneNumbers.push(phoneNumber);
}
// basic information
this.bas.$id = b['id'];
this.bas.$name = b['name'];
this.bas.$birthDate = b['birthDate'];
this.bas.$gender = b['gender'];
this.bas.$blood_Group = b['blood_Group'];
this.bas.$religion = b['religion'];
this.bas.$email = b['email'];
});
}// get current user basic.
public getBasicInformation() {
return this.bas;
}
组件
public setBasicInformation() {
try {
var basic: Basic = this.basicService.getBasicInformation();
this.name = basic.$name;
console.log(this.name);
console.log('\n\n name : ' + basic['name']);
console.log('\n\n $name : ' + basic.$name);
this.birthDate = basic.$birthDate;
this.gender = basic.$gender;
console.log(basic);
} catch (e) {
console.log('Error in Setting Basic Information!');
}
}
以下几行正在打印undefined
。
console.log(this.name);// should not work, as property is private in model class.
console.log('\n\n name : ' + basic['name']);
console.log('\n\n $name : ' + basic.$name);
模型类
export class Basic {
private id: string;
private userId: string;
private name: string;
private birthDate: string;
private care_of: string;
private gender: string;
private maritalStatus: string;
private profession: string;
private blood_Group: string;
private available: string;
private religion: string;
private email: string;
/**
* Getter $id
* @return {string}
*/
public get $id(): string {
return this.id;
}
/**
* Getter $userId
* @return {string}
*/
public get $userId(): string {
return this.userId;
}
/**
* Getter $name
* @return {string}
*/
public get $name(): string {
return this.name;
}
/**
* Getter $birthDate
* @return {string}
*/
public get $birthDate(): string {
return this.birthDate;
}
/**
* Getter $care_of
* @return {string}
*/
public get $care_of(): string {
return this.care_of;
}
/**
* Getter $gender
* @return {string}
*/
public get $gender(): string {
return this.gender;
}
/**
* Getter $maritalStatus
* @return {string}
*/
public get $maritalStatus(): string {
return this.maritalStatus;
}
/**
* Getter $profession
* @return {string}
*/
public get $profession(): string {
return this.profession;
}
/**
* Getter $blood_Group
* @return {string}
*/
public get $blood_Group(): string {
return this.blood_Group;
}
/**
* Getter $available
* @return {string}
*/
public get $available(): string {
return this.available;
}
/**
* Setter $id
* @param {string} value
*/
public set $id(value: string) {
this.id = value;
}
/**
* Setter $userId
* @param {string} value
*/
public set $userId(value: string) {
this.userId = value;
}
/**
* Setter $name
* @param {string} value
*/
public set $name(value: string) {
this.name = value;
}
/**
* Setter $birthDate
* @param {string} value
*/
public set $birthDate(value: string) {
this.birthDate = value;
}
/**
* Setter $care_of
* @param {string} value
*/
public set $care_of(value: string) {
this.care_of = value;
}
/**
* Setter $gender
* @param {string} value
*/
public set $gender(value: string) {
this.gender = value;
}
/**
* Setter $maritalStatus
* @param {string} value
*/
public set $maritalStatus(value: string) {
this.maritalStatus = value;
}
/**
* Setter $profession
* @param {string} value
*/
public set $profession(value: string) {
this.profession = value;
}
/**
* Setter $blood_Group
* @param {string} value
*/
public set $blood_Group(value: string) {
this.blood_Group = value;
}
/**
* Setter $available
* @param {string} value
*/
public set $available(value: string) {
this.available = value;
}
/**
* Getter $religion
* @return {string}
*/
public get $religion(): string {
return this.religion;
}
/**
* Setter $religion
* @param {string} value
*/
public set $religion(value: string) {
this.religion = value;
}
/**
* Getter $email
* @return {string}
*/
public get $email(): string {
return this.email;
}
/**
* Setter $email
* @param {string} value
*/
public set $email(value: string) {
this.email = value;
}
}//class
据我所知,这两行应显示值:
console.log('\n\n name : ' + basic['name']);
console.log('\n\n $name : ' + basic.$name);
此console.log(basic);
正在打印值时。
答案 0 :(得分:0)
我认为您在服务器返回elements
函数中的响应之前正在调用setBasicInformation()
。
在通过引用传递Javascript对象的情况下,您在getCurrentUserBasic()
中的var basic
引用了服务中的setBasicInformation()
。
当您在控制台上记录一个对象时,它将在基础对象更新时更新log的值,例如一旦在服务中更新了console.log(基本),即使在控制台上,它也会开始反映this.bas的值。
虽然console.log中没有字符串,但它始终会在调用console.log时记录绝对值。
在basic的值为null时,以下两行记录字符串,因此它们的记录未定义。
this.bas
更改console.log('\n\n name : ' + basic['name']);
console.log('\n\n $name : ' + basic.$name);
以在setBasicInformation()
的订阅fn中设置值,并通过getCurrentUserBasic
传递值
getBasicInformation()