[英]ES5 vs ES6 Javascript for Arrays
我正在嘗試將以下內容轉換為工作方式,但我認為這是Es5 / ES6問題。 幫助表示贊賞。
this.rows = Array.from(Array(Math.ceil(this.subCategoryModels.length / 3)).keys());
我目前收到打字錯誤:
[ts]
Property 'from' does not exist on type 'ArrayConstructor'.
any
和
[ts]
Property 'keys' does not exist on type 'any[]'.
any
類型“ ArrayConstructor”上不存在屬性“ from”。
這表明您希望在仍編譯為ES5的同時使用ES6
類型定義。
推薦最新的TypeScript中的lib
選項:
"compilerOptions": {
"target": "es5",
"lib": ["es6", "dom"]
}
https://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html#lib-option
我最終做了以下工作:
html
<ion-row *ngFor="let trio of getTriples()">
<ion-col *ngFor="let item of trio" (click)="itemTapped(item)">
<div class="row responsive-md">
<div id="icon-image-{{item.id}}"><img src="data:image/png;base64,{{item.icon}}" height="75" width="75" /></div>
<p>{{item.name}}</p>
</div>
</ion-col>
</ion-row>
打字稿
getTriples() {
let triples = [];
let length = this.subCategoryModels.length;
for (let i = 0; i < length; i += 3) {
let trio = [];
trio.push(this.subCategoryModels[i]);
if (i + 1 < length) {
trio.push(this.subCategoryModels[i + 1]);
}
if (i + 2 < length) {
trio.push(this.subCategoryModels[i + 2]);
}
triples.push(trio);
}
return triples;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.