Búsqueda de sitios web

typescript - Angular webpack: Error al compilar


Estoy aprendiendo Angular 5 con el tutorial oficial. Estoy leyendo el capítulo Servicio: Capítulo Servicio

Ahora, estoy aquí en el capítulo:

Mírala correr

Después de que el navegador se actualice, la aplicación debería funcionar como antes, mostrando una lista de héroes y una vista de detalles del héroe cuando hagas clic en el nombre de un héroe.

Y tengo este error:

    ERROR in ./src/app/heroes/heroes.component.ts
    Module parse failed: Unexpected token (18:34)
    You may need an appropriate loader to handle this file type.
|     function HeroesComponent(heroService) {
|         this.heroService = heroService;
|         this.heroes = hero_1.Hero[];
|     }
|     HeroesComponent.prototype.ngOnInit = function () {
 @ ./src/app/app.module.ts 12:25-61
 @ ./src/main.ts
 @ multi webpack-dev-server/client?http://0.0.0.0:0 ./src/main.ts*

webpack: Failed to compile.
ERROR in src/app/heroes/heroes.component.ts(13,17): error TS1109: Expression expected.

En realidad, solo seguí el tutorial y tengo este error.

Aquí, los archivos:

src/app/app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { HeroesComponent } from './heroes/heroes.component';

import { FormsModule } from '@angular/forms';
import { MagiciansComponent } from './magicians/magicians.component';
import { HeroDetailComponent } from './hero-detail/hero-detail.component';

import { HeroService } from './hero.service';
import { MessageService } from './message.service';

@NgModule({
  declarations: [
    AppComponent,
    HeroesComponent,
    MagiciansComponent,
    HeroDetailComponent
  ],
  imports: [
    BrowserModule,
    FormsModule
  ],
  providers: [HeroService, MessageService],
  bootstrap: [AppComponent]
})
export class AppModule { }

src/app/heroes.component.ts

import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero';
import { HEROES } from '../mock-heroes';
import { HeroService } from '../hero.service';

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {

        heroes =  Hero[];
        selectedHero: Hero;

        constructor(private heroService: HeroService) {}

        ngOnInit() {
                this.getHeroes();
        }

        onSelect(hero: Hero): void {
                this.selectedHero = hero;
        }

        getHeroes(): void {
                this.heroes = this.heroService.getHeroes();
        }
}

Gracias por tu ayuda

Respuesta #1

Tu propiedad heroes fue declarada incorrectamente en heroes.component.ts.

Tienes:

heroes = Hero[];

Debería ser:

heroes: Hero[];

Un error común: en otros lenguajes = es válido al asignar un valor a una variable de instancia, sin embargo, dado que TypeScript se basa en JavaScript (por ejemplo, usa objetos JSON) se utiliza : para lograr esto.

Respuesta #2

Intenta cambiar esto heroes=Hero[] a:

heroes: Array<Hero> = [];

Artículos relacionados