繁体   English   中英

阵列的ES5与ES6 Javascript

[英]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

由于您看到了这些错误,因此我假设您正在编译到ES5。 您可以使用分型 ,使打字稿识别ES2015功能阵列。

首先,安装分型如果尚未安装。

npm install typings --global

安装类型后,请为ES2015数组函数安装类型类型定义。

typings install -SG env~es2015-array

如果您要定位的浏览器不支持新的Array函数,则还需要添加一个polyfill。

我最终做了以下工作:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM