如何在打字稿中将对象推入数组对象?

时间:2018-11-15 12:28:33

标签: javascript arrays angular

import {Plan} from './plan.model';

export class PlanService{

  private plans:Plan[]=[
    new Plan("5:00","reading"),
    new Plan("6:00","writing"),
    new Plan("7:00","cleaning")
  ];

  getPlan(){
    return this.plans.slice();//returns the plan 
  }

  setPlan(time,plan){
    this.plans.push(new Plan(time,plan));
  } 
}

我在上面尝试过将新的Plan对象推入定义的数组中,但这不起作用。

1 个答案:

答案 0 :(得分:0)

这可能对您有帮助

`setPlan(time,plan){
this.plans.push(new plan(){time : time , plan : plan})
};`