Angular2:错误:模块'AppModule'声明的意外值'AppComponent'

0_X*_*BNZ 25 angular

我正在编写https://angular.io/docs/ts/latest/tutorial/toh-pt2.html上的教程,并在创建英雄数组后得到以下错误:

Error: Error: Unexpected value 'AppComponent' declared by the module 'AppModule'
    at new BaseException (localhost:3000/node_modules/@angular/compiler//bundles/compiler.umd.js:5116:27)
    at eval (localhost:3000/node_modules/@angular/compiler//bundles/compiler.umd.js:13274:35)
    at Array.forEach (native)
    at CompileMetadataResolver.getNgModuleMetadata (localhost:3000/node_modules/@angular/compiler//bundles/compiler.umd.js:13261:53)
    at RuntimeCompiler._compileComponents (localhost:3000/node_modules/@angular/compiler//bundles/compiler.umd.js:15845:51)
    at RuntimeCompiler._compileModuleAndComponents (localhost:3000/node_modules/@angular/compiler//bundles/compiler.umd.js:15769:41)
    at RuntimeCompiler.compileModuleAsync (localhost:3000/node_modules/@angular/compiler//bundles/compiler.umd.js:15746:25)
    at PlatformRef_._bootstrapModuleWithZone (localhost:3000/node_modules/@angular/core//bundles/core.umd.js:9991:29)
    at PlatformRef_.bootstrapModule (localhost:3000/node_modules/@angular/core//bundles/core.umd.js:9984:25)
    at Object.eval (localhost:3000/app/main.js:4:53)
Evaluating localhost:3000/app/main.js
Error loading localhost:3000/app/main.js
Run Code Online (Sandbox Code Playgroud)

我的app.module.ts文件此时是:

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

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

@NgModule({
    imports:        [ BrowserModule, FormsModule ],
    declarations:   [ AppComponent ],
    bootstrap:      [ AppComponent ]
})

export class AppModule {}
Run Code Online (Sandbox Code Playgroud)

我的app.components.ts文件此时是:

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

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

@Component ({
    selector: 'my-app',
    template: `
        <h1>{{title}}</h1>

        <h2>My Heroes</h2>
        <ul class="heroes">
            <li *ngFor="let hero of heroes">
                 <span class="badge">{{hero.id}}</span> {{hero.name}}
            </li>
         </ul>        

         <h2>{{hero.name}} details!</h2>
         <div><label>id: </label>{{hero.id}}</div>
         <div>
             <label>name: </label>
             <input [(ngModel)]="hero.name" placeholder="name">
         </div> 
         ` 
 })

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' }
];

export class AppComponent {
    title = 'Tour or Heroes';
    heroes = HEROES;
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切都在努力.Angular的新手,不知道如何调试它.

编辑:

错误发生在(index)at:

    <!-- 2. Configure SystemJS -->
    <script src = "systemjs.config.js"></script>
    <script>
        System.import('app').catch(function(err) {
            console.error(err);
        });
    </script>
</head>
Run Code Online (Sandbox Code Playgroud)

谢谢,

cab*_*rog 34

装饰器是遵循装饰器声明的相关类或变量.

在你的代码中,@compoment装饰器是const HEROES,这必须是类AppComponent.

所以你的代码应该如下:

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

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


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' }
];


@Component ({
    selector: 'my-app',
    template: `
        <h1>{{title}}</h1>

        <h2>My Heroes</h2>
        <ul class="heroes">
            <li *ngFor="let hero of heroes">
                 <span class="badge">{{hero.id}}</span> {{hero.name}}
            </li>
         </ul>        

         <h2>{{hero.name}} details!</h2>
         <div><label>id: </label>{{hero.id}}</div>
         <div>
             <label>name: </label>
             <input [(ngModel)]="hero.name" placeholder="name">
         </div> 
         ` 
 })
export class AppComponent {
    title = 'Tour or Heroes';
    heroes = HEROES;
}
Run Code Online (Sandbox Code Playgroud)

  • +1.因为在一个教程片段中只给出了_Hero_类的代码,所以我将它放在@Component下面.他们应该在那里做一个提醒,以警告初学者. (3认同)
  • @LucasMedina是的,每个装饰器都与以下声明相关联. (2认同)

nik*_*ong 16

我会很快注意到这个错误可以从任何@Component声明错误的方式抛出- 即使是@Component({});- (注意分号).

如果收到此错误,请确保检查语法是否正确.

从2.0.0-rc.6开始为真.