简体   繁体   English

如何访问另一个类/ TypeScript 中的类

[英]How can I access a class inside another class / TypeScript

I have my main class A我有我的主班A

export class A {

    public sourceReference: string;

    public charge: Charge;
}

my Charge class looks like such:我的 Charge 类看起来像这样:

export class Charge {

    public chargeType: string;
    
    public chargeAccountNumber: string;

    public chargeCurrency: string;
}

In my index file I want to use my main class A and populate my payload as follows:在我的索引文件中,我想使用我的主类 A 并按如下方式填充我的有效负载:

    import { A } from "./a";
    
    public static create(payload) {

      let myData = new A();

      myData.sourceReference = payload.sourceRef;
      myData.charge.chargeType = payload.type;
      myData.charge.chargeAccountNumber = payload.accountNumber;
      myData.charge.chargeCurrency = payload.currency

      return JSON.stringify(myData)

     }

my incoming payload file looks like such:我传入的有效负载文件如下所示:

payload = { sourceRef: "1234", type: '615-01', accountNumber: '123456', currency: 'USD'}

I am able to generate myData.sourceReference我能够生成myData.sourceReference

However,然而,

The problem is that myData.charge is returning as undefined, resulting in undefined values for myData.charge.chargeType , myData.charge.chargeAccountNumber & myData.charge.chargeCurrency .问题是myData.charge返回未定义,导致myData.charge.chargeTypemyData.charge.chargeAccountNumbermyData.charge.chargeCurrency值未定义。

Exception: TypeError: Cannot set property 'chargeType' of undefined异常:TypeError:无法设置未定义的属性“chargeType”

Class A has a property of the class Charge .A拥有类的属性Charge

Having said that, the charge property only has a type - but no instance.话虽如此, charge属性只有一个类型——但没有实例。

The class A will have to create an instance of the class Charge somewhere - mostly in the constructor.A必须在某处创建类Charge的实例——主要是在构造函数中。

The constructor should look like this:构造函数应如下所示:

export class A {

  public sourceReference: string;
  public charge: Charge;

  constructor() {
     this.charge = new Charge()
  }

 ...

The exception is telling you what's the problem.例外是告诉您问题出在哪里。 You have to define myData.charge, using myData.charge = new Charge() and then define it's properties like您必须使用myData.charge = new Charge()定义 myData.charge,然后定义它的属性,例如

myData.charge.chargeType = payload.type;
myData.charge.chargeAccountNumber = payload.accountNumber;
myData.charge.chargeCurrency = payload.currency

Or if you want to be fancy, you can create a constructor for charge like this:或者,如果你想花哨,你可以像这样创建一个用于充电的构造函数:

export class Charge {
    
    constructor(chargeType:string, chargeAccountNumber:string, chargeCurrency:string){
    this.chargeType = chargeType;
    this.chargeAccountNumber = chargeAccountNumber;
    this.chargeCurrency=chargeCurrency;
    }

    public chargeType: string;

    public chargeAccountNumber: string;

    public chargeCurrency: string;
}

And then you can call the constructor like然后你可以像这样调用构造函数

myData.charge = new Charge(chargeType,chargeAccountNumber,chargeCurrency) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在该类的另一个函数中使用在Typescript类中声明的函数? - How can I use a function declared inside a Typescript class inside another function in that class? 如何访问类函数中的类变量? - How can I access a class variable inside a class function? 如何从类外部访问类中的方法 - How can I access methods inside a class from outside of class 如何使用Typescript将类变量传递到函数内部的函数中? - How can I pass a class variable into a function inside a function with typescript? 如何在$ http调用的.success内部访问类属性? - How can I access class properties inside the .success of an $http call? 如何从类中的另一个函数调用函数 - How can I call a function from another function inside a Class 如何从 Ajax 在打字稿中调用的函数内部调用类中的函数? - How can I call a function inside a class from inside a function called up by Ajax in typescript? 为什么我不能在打字稿类中定义接口或类型 - why i can't define interface or type inside typescript class 在TypeScript中无法访问lambda中的类作用域? - Access to class scope inside lambda in TypeScript is undefined? 如何在不使用 this 关键字的情况下访问 Javascript 中同一类的方法内的类的构造函数中定义的属性? - How can I access a property defined in a constructor of a class inside a method of the same class in Javascript without using this keyword?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM