首页 > 解决方案 > 在角度 10 中创建对象的对象时出错

问题描述

export class School {
    constructor(
        
        public name: string,
        public address: Address,
        public district: string,
        public contactNumber: string,
        public gradeRange: string,
        public currentYear: number,
        public trialAcct: boolean,
        public customerInfo: CustomerInfo,
        public composing: boolean,
        public createdAt: number,
        
    ){}
    export class Address{
            streetAddress1: string;
            streetAddress2:string;
            city: string;
            state: string;
            postalCode: string;

    }

    export class CustomerInfo{
        public customerID: string;
        public paymentID: string;
        public subscriptionID: string;
        public maskedPayment: string;
        public firstName: string;
        public lastName: string;
    }

那是我为数据定义的模型,当使用我从 api 获得的数据创建此类的对象时,它没有提供给我的地址。但在数据地址中是提供的。这就是我创建这个对象的方式,

getSchool(): School {
    return new School(this.schooldata);
  }

this.schooldata 中包含数据。

我不知道我应该在哪里得到错误

标签: angularangular-directive

解决方案


最好使用接口并传递它。

export interface objProp{
   name: string,
   address: Address,
   district: string,
   contactNumber: string,
   gradeRange: string,
   currentYear: number,
   trialAcct: boolean,
   customerInfo: CustomerInfo,
   composing: boolean,
   createdAt: number,
}

推荐阅读