Angular 2类型脚本错误-TS2322



我正在研究Angular 2平均应用程序项目。我在代码中面对以下问题。

错误ts2322:type'{'id':string;'Bowlingteam':弦; 'thingteam':string;}'不能分配键入'任何[]。
属性'包括'{'id':string;"保龄球": 细绳;'thingteam':string;}'

我已将类变量声明为

startMatchInput : Array<any> = [{
       'id' : '',
       'bowlingteam' : '',
       'battingteam' : ''
  }];

我使用班级中声明的函数列出的每个对象。基本上,我正在形成一个单个对象。

以下是函数中的代码。

function x(){
  var startMatchmasterObj = {
                'id' : '',
                'bowlingteam' : '',
                'battingteam' : ''
          };
startMatchmasterObj.id =  "943974937947";
               startMatchmasterObj.bowlingteam =  "098idsjvlnladfsj";
               startMatchmasterObj.battingteam =  "jzvlzc9a7dfs90as";
 this.startMatchInput = startMatchmasterObj; // here error is coming
}

我将我的类变量分配了从该函数的本地变量,以便在需要时在外面访问它。但是面对上错误。

任何帮助或指示都将不胜感激。我是Angular 2的新手以及平均堆栈开发。

谢谢....

startMatchInput的类型是any[]startMatchmasterObj是一个对象,而不是数组,因此它们的类型不兼容,因此错误。因此,您可以将StartMatchInput的类型更改为any,也可以将startMatchmasterObj包装在数组中。

startMatchInput: any = {    // declare it as any, rather than any[]
   'id' : '',
   'bowlingteam' : '',
   'battingteam' : ''
};    
// or...
this.startMatchInput = [startMatchMasterObj];    // wrap in an array

您的最佳选择将取决于您希望如何使用它。

最新更新