【发布时间】:2021-07-16 13:45:38
【问题描述】:
我有一个使用 Angular-12 的代码。如下图:
界面:
export interface ICandidate {
id: number;
first_name: string;
other_name: string;
last_name : string;
email: string;
gender : string;
user_photo: any;
marital_status: string;
dob : Date;
address : string;
cv_file: any;
achievement: IAchievement[];
certificate: ICertificate[];
education: IEducation[];
experience: IExperience[];
skills: ISkill[];
}
candidate.service:
import { ICandidate, IAchievement, ICertificate, IEducation, IExperience, ISkill } from '../models/candidate.model';
@Injectable({
providedIn: 'root'
})
export class CandidateService {
constructor(
private http: HttpClient,
private token: TokenService,
private api: ApiService
) { }
private candidateDetails!: ICandidate;
getCandidateDetails(): ICandidate {
return this.candidateDetails;
}
setCandidateDetails(candidateDetails: ICandidate): void {
this.candidateDetails = candidateDetails;
}
getCandidateProfile(): Observable<ICandidate> {
let headers = new HttpHeaders();
headers = headers.set('Authorization', this.token.get());
const url: string = this.api.baseURL + 'display';
return this.http.get<ICandidate>(url, { headers });
}
}
组件:
import { Component, OnInit } from '@angular/core';
import { CandidateService } from 'src/app/features/driver/services/candidate.service';
import { ICandidate } from 'src/app/features/driver/models/candidate.model';
import { AppState } from 'src/app/store/reducers';
@Component({
selector: 'app-profile-list',
templateUrl: './profile-list.component.html',
styleUrls: ['./profile-list.component.scss']
})
export class ProfileListComponent implements OnInit {
public loggedIn!: boolean;
candidateDetails!: ICandidate;
constructor(
private store: Store<fromStore.AppState>,
private router: Router,
private auth: AuthService,
private token: TokenService,
private api : ApiService,
private candidateService: CandidateService,
) {
}
ngOnInit(): void {
this.candidateService.getCandidateProfile().subscribe(
(response) => {
console.log(response);
this.candidateDetails = response;
console.log(this.candidateDetails);
this.candidateService.setCandidateDetails(this.candidateDetails);
});
}
}
当我做 console.log(response);在组件中,我得到了:
{
"message": "Profile Successfully Retrieved.",
"error": false,
"code": 200,
"results": {
"profile": {
"id": 2,
"user_type": "Teacher",
"created_at": "2021-07-07T07:19:13.000000Z",
"updated_at": "2021-07-15T09:57:48.000000Z",
"deleted_at": null,
"last_login_at": "2021-07-15T09:57:48.000000Z",
"detail": {
"id": 1,
"user_id": 2,
"first_name": "Lamptey",
"last_name": "Akwetey",
"other_name": null,
"email": "lamptey@yahoo.com",
"gender": null,
"user_photo": null,
"marital_status": null,
"dob": null,
"address": null,
"cv_file": null,
"summary": null,
"created_at": "2021-07-07T07:19:13.000000Z",
"updated_at": null
},
"educations": [],
"experiences": [],
"achievements": [],
"certificates": [],
"skills": [],
"employees": []
}
}
profile 和 detail 具有单一数据。
现在我想显示个人资料、详细信息、教育和经历
{{ CandidateDetails.results.profile.user_type }}
给出这个错误:
类型“ICandidate”.ngt 上不存在属性“结果”
同样
{{ CandidateDetails.results.profile.detail.first_name }}
我该如何解决这个问题?
谢谢
【问题讨论】:
-
return this.http.get
(url, { headers }).pipe(map(response => response.results.profile)) -
正如@MikeOne 在我的回答中提到的,响应似乎与定义的接口不匹配。例如。界面中没有
results属性。修复后,您可以使用安全导航运算符:{{ candidateDetails?.results?.profile?.user_type }}在尝试访问它的属性之前检查变量是否已定义。或者在任何属性未定义时执行{{ candidateDetails.results.profile.user_type || '-' }}以显示其他内容(如-)。
标签: angular