最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to initialize a typed Object in TypeScriptAngular? - Stack Overflow

programmeradmin0浏览0评论

I'm new to Angular & TypeScript and trying to figure out how to instantiate an object (before an api request is returned with the real data).

For example, my model looks like this:

//order.model.ts
export class Order {
  constructor(public id: number, currency: string, public contact: Object, public items: Array<Object>) {}
}

And then I try to instantiate that in one of my ponents, let's say the App ponent:

//appponent.ts
export class AppComponent {
  @Input()
  public order: Order = new Order();
}

Of course, it expected to receive 4 arguments when instantiating new Order() but received 0. Do I actually have to pass in undefined/empty values for each attribute of Order?

In good ol' React (without TS) I would just initialize with an empty object and call it a day:

this.state = {
 order: {}
}

What's best practice for this sort of thing in Angular/TS?

I'm new to Angular & TypeScript and trying to figure out how to instantiate an object (before an api request is returned with the real data).

For example, my model looks like this:

//order.model.ts
export class Order {
  constructor(public id: number, currency: string, public contact: Object, public items: Array<Object>) {}
}

And then I try to instantiate that in one of my ponents, let's say the App ponent:

//app.ponent.ts
export class AppComponent {
  @Input()
  public order: Order = new Order();
}

Of course, it expected to receive 4 arguments when instantiating new Order() but received 0. Do I actually have to pass in undefined/empty values for each attribute of Order?

In good ol' React (without TS) I would just initialize with an empty object and call it a day:

this.state = {
 order: {}
}

What's best practice for this sort of thing in Angular/TS?

Share Improve this question asked Mar 26, 2019 at 18:43 Ryan SperzelRyan Sperzel 9054 gold badges10 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Yes as it is currently set up you would have to pass 4 default arguments to the constructor.

public order: Order = new Order(1, '', {}, []);

Or you can set each property as nullable by adding a ? like so:

export class Order {
  constructor(public id?: number, currency?: string, public contact?: Object, public items?: Array<Object>) {}
}

If the class doesn't have functionality (you are simply using it for type checking) the best way to do it would be to declare an interface like so (you can also make them nullable here with ?s):

export interface Order {
    id: number;
    currency: string;
    contact: Object;
    items: Object[];
}

then in your ponent do not initialize the value until you have all of the needed values:

//app.ponent.ts
export class AppComponent {
  @Input()
  public order: Order;

  // just an example
  setValues(id: number, currency: string, contact: Object, items: Object[]) {
    this.order = {
      id: id,
      currency: currency,
      contact: contact,
      items: items
    }
  }

  // example for if you receive object with correct fields from backend
  getData() {
    this.service.getData().subscribe(result => {
       this.order = result;
    });
  }
}
发布评论

评论列表(0)

  1. 暂无评论