javascript - 解决 promise angular2 : Property 'includes' does not exist on type '[]'

标签 javascript angular angular2-services

我遇到了错误

app/hero-detail.component.ts(44,29): error TS2339: Property 'includes' does not exist on type '[]'.
app/hero.service.ts(25,25): error TS1122: A tuple type element list cannot be empty.

代码不会编译运行,这个错误在npm start .

英雄.service.ts:

import { Injectable } from '@angular/core';

import { Hero, HeroIds } from './hero';
import { HEROES } from './mock-heroes';

@Injectable()
export class HeroService {

  getHeroes(): Promise<Hero[]> {
    return Promise.resolve(HEROES);
  }

  getHero(id: number): Promise<Hero> {
    return this.getHeroes()
        .then(heroes => heroes.find(hero => hero.id === id));
  }

  getHeroesSlowly(): Promise<Hero[]> {
    return new Promise(resolve => {
      // Simulate server latency with 2 second delay
      setTimeout(() => resolve(this.getHeroes()), 2000);
    });
  }

  getHeroIds(): Promise<[]> {
    return this.getHeroes()
    .then(heroes => heroes.map(function(item) { return item.id; }));
  }
}

hero-detail.component.ts:

import { Component, Input, OnInit } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { Location } from '@angular/common';

import 'rxjs/add/operator/switchMap';
import { FlashMessagesService } from 'angular2-flash-messages';

import { Hero, HeroIds }         from './hero';
import { HeroService } from './hero.service';


@Component({
  moduleId: module.id,
  selector: 'my-hero-detail',
  templateUrl: './static/templates/hero-detail.component.html',
  styleUrls: ['./static/css/hero-detail.component.css'],
})
export class HeroDetailComponent implements OnInit {
    @Input() hero: Hero;
    hero_ids: Array<HeroIds> = [];

    constructor(
        private heroService: HeroService,
        private route: ActivatedRoute,
        private location: Location,
        private router: Router,
        private _flashMessagesService: FlashMessagesService
    ) { }

    ngOnInit(): void {
        this.route.params
            .switchMap((params: Params) => this.heroService.getHero(+params['id']))
            .subscribe(hero => this.hero = hero);
    }

    goBack(): void {
        this.location.back();
    }

    gotoDetail(hero_id: string): void {
        this.heroService.getHeroIds()
        .then(
            result => {
                if ( result.includes(+hero_id) ) {
                    this.router.navigate(['/detail', hero_id]);
                } else {
                    this._flashMessagesService.show("Please pick a valid hero ID");
                }
            }
        );
    }
}

模拟英雄.ts:

import { Hero } from './hero';

export const HEROES: Hero[] = [
  {id: 11, name: 'Mr. Nice'},
  {id: 12, name: 'Narco'},
  {id: 13, name: 'Bombasto'},
  {id: 14, name: 'Celeritas'},
  {id: 15, name: 'Magneta'},
  {id: 16, name: 'RubberMan'},
  {id: 17, name: 'Dynama'},
  {id: 18, name: 'Dr IQ'},
  {id: 19, name: 'Magma'},
  {id: 20, name: 'Tornado'}
];

英雄.ts:

export class Hero {
  id: number;
  name: string;
}

export class HeroIds {
    id: number;
}

删除 Promise<Hero[]>部分来自 getHeroIds只是原因

app/hero.service.ts(19,5): error TS1131: Property or signature expected.
app/hero.service.ts(23,3): error TS1128: Declaration or statement expected.
app/hero.service.ts(25,15): error TS1005: ';' expected.
app/hero.service.ts(26,12): error TS1005: ':' expected.
app/hero.service.ts(27,68): error TS1005: ',' expected.
app/hero.service.ts(29,1): error TS1128: Declaration or statement expected.

最佳答案

Array.prototype.includes() 是 ES2016 规范 ( link ) 的一部分。你必须在 TypeScript 的编译中包含这个库。在你的 tsconfig.json 添加

"compilerOptions": {
  "lib": [ "es2016" ]
}

它应该可以工作。

关于javascript - 解决 promise angular2 : Property 'includes' does not exist on type '[]' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42866350/

相关文章:

javascript - 无法使用服务

java - 如何开始使用 Java 服务器后端构建 Facebook 应用程序?

javascript - 使用 Node.js 应用程序中的 Request 模块发出 API 请求

javascript - Stack Overflow 样式扩展标签信息?

angular - AoT NGC/Angular2 : Property is protected and only accessible within class Error

javascript - 内容丰富 + Angular2

javascript - jQuery:防止动画和超时排队?

angular - react 形式的 formGroup.get 与 formGroup.controls - Angular

Angular Material 2 日期选择器自动打开焦点

javascript - 在 Angular 2.0 的 Heroes 示例中使用实时 API 需要进行哪些更改