无法创建行为主题

时间:2018-07-09 07:34:59

标签: typescript angular6

我无法创建BehaviourSubject

我期望有一个json可以映射到以下Typescript

export class GetDataAPI {
  'some-data':string;
  constructor (public title:string,
               public description:string,
               public tags:Array<string>,
               public someData:string
             ){
    this['some-data'] = someData;
  }
}

在我的代码中,我创建了一个BehaviourSubject,如下所示:

private dataSubject: BehaviorSubject<GetDataAPI>;

当我尝试初始化BehaviourSubject时,出现编译错误this.dataSubject = new BehaviorSubject<GetDataAPI>({});

错误为error TS2345: Argument of type '{}' is not assignable to parameter of type 'GetDataAPI'. Property ''some-data'' is missing in type '{}'.

1 个答案:

答案 0 :(得分:0)

我必须创建一个指定所有参数的对象,即

new BehaviorSubject<GetDataAPI>({'some-data':'',
      'title':'',
  'description':'',
  'tags':[],
  'someData':''});