【发布时间】:2020-06-04 17:18:39
【问题描述】:
我已经将 Angular 从 4 更新到 9,现在,在能够让它工作之后,仍然遇到一些我无法解决的错误。
我的代码:
this.getTrades().then( ( trades ) => {
console.log( trades );
this.trades = new MatTableDataSource<Trade>( trades );
});
getTrades() {
let promise = new Promise( ( resolve, reject ) => {
this.dataService.getTrades().subscribe( ( trades ) => {
resolve( trades );
});
});
return promise;
}
export interface Trade {
ID: number;
UserID: number;
DateTime: Date;
Exchange: Exchange;
BaseCoin: Coin;
MarketCoin: MarketCoin;
Price: number;
Amount: number;
Total: number;
Last: number;
Type: Type;
Status: Status;
Symbol: string;
}
getTrades() 数据源:
getTrades() {
return this.http.get( 'http://localhost:8888/currencytracker-api/json/get-trades.php' ).pipe(
map( res => res.json() ));
}
getTrades() 返回一个包含以下数据的 json 数组:
ID: 1
UserID: 1
DateTime: "2017-12-25T00:00:00+0000"
Exchange: {ID: 1, Title: "BitTrex"}
BaseCoin: {ID: 718, Abbr: "BTC"}
MarketCoin: {ID: 743, Abbr: "XVG"}
Price: "0.000013470000"
Amount: "1019.014328640000"
Total: 0.0137261230067808
Last: "0.000005470000"
Type: {ID: 1, Title: "Limit Buy"}
Status: {ID: 2, Title: "Closed"}
Symbol: "Ƀ"
这是我得到的错误:
src/app/components/trades/trades.component.ts:100:68 中的错误 - 错误 TS2345:“未知”类型的参数不可分配给“交易[]”类型的参数。类型“{}”缺少类型“Trade[]”的以下属性:length、pop、push、concat 等 26 个。
this.trades = new MatTableDataSource( trades );
任何帮助理解此错误并解决它将不胜感激。谢谢。
更新
当我声明交易变量时,我是这样做的:
Trade[]
相反,我现在将其声明为:
MatTableDataSource<Trade>
【问题讨论】:
标签: angular typescript casting