Angular 角度2/4/5/6如何获取对象属性
我有一个api调用,但是我似乎在检索对象中的属性时遇到了问题 步骤组件.tsAngular 角度2/4/5/6如何获取对象属性,angular,typescript,object,Angular,Typescript,Object,我有一个api调用,但是我似乎在检索对象中的属性时遇到了问题 步骤组件.ts ngOnInit(){ this.stepService.getSteps().subscribe(appconfig => { console.log(appconfig); //this returns me an object console.log(appconfig.isstepcontactdisabled); // this gives me undefine
ngOnInit(){
this.stepService.getSteps().subscribe(appconfig => {
console.log(appconfig); //this returns me an object
console.log(appconfig.isstepcontactdisabled); // this gives me undefined
}
}
getSteps(): Observable<Step> {
return this.http.get<Step>(appConfig.apiBasePath + "appconfig").pipe(
catchError(this.handleError),
);
}
export interface Step {
appconfig: Appconfig;
}
export interface Appconfig {
isstepcontactdisabled: boolean;
isstepdevicebranddisabled: boolean;
issteppaymentinfodisabled: boolean;
isstepservicetypedisabled: boolean;
}
步骤服务.ts
ngOnInit(){
this.stepService.getSteps().subscribe(appconfig => {
console.log(appconfig); //this returns me an object
console.log(appconfig.isstepcontactdisabled); // this gives me undefined
}
}
getSteps(): Observable<Step> {
return this.http.get<Step>(appConfig.apiBasePath + "appconfig").pipe(
catchError(this.handleError),
);
}
export interface Step {
appconfig: Appconfig;
}
export interface Appconfig {
isstepcontactdisabled: boolean;
isstepdevicebranddisabled: boolean;
issteppaymentinfodisabled: boolean;
isstepservicetypedisabled: boolean;
}
这是我的结果(在step.component.ts中的console.log,我没有定义)
我尝试使用appconfig.isstepcontactdisabled和appconfig[“isstepcontactdisabled”]获取属性,但它仍然显示为未定义。有人能启发我吗?谢谢你的帮助 您需要使用
appconfig.appconfig.isstepcontactdisabled
,或appconfig['appconfig']['isstepcontactdisabled']
对象有点嵌套,因此您可能错过了appconfig
属性
尝试在其上运行
console.log(appconfig.appconfig.isstepcontactdisabled)
不过有一点建议,如果您使用Step.ts纯粹是为了进行类型检查,那么您可能希望使用接口而不是类。