【问题标题】:Angular2: Error: unexpected value 'AppComponent' declared by the module 'AppModule'Angular2:错误:模块“AppModule”声明的意外值“AppComponent”
【发布时间】:2016-09-01 00:02:16
【问题描述】:

我正在阅读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

此时我的 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 {}

此时我的 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;
}

到目前为止一切正常。 Angular 新手,不知道如何调试。

编辑:

错误发生在(索引)处:

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

谢谢,

【问题讨论】:

    标签: angular


    【解决方案1】:

    装饰器是在装饰器声明之后的相关类或变量。

    在您的代码中,@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;
    }
    

    【讨论】:

    • +1。因为在一个教程 sn-p 中只给出了类 Hero 的代码,所以我将它放在 @Component 下方。他们应该在那里做笔记以警告初学者。
    • 这很好,只是代码无法运行,因为 hero 变量存在问题,不存在
    • @raghav710 我做了同样的事情。那么在装饰器之后所做的每个声明都与之相关联?我想我会对此进行一些研究。
    • @LucasMedina 是的,每个装饰器都与以下声明相关联。
    【解决方案2】:

    我会很快注意到,任何形式的@Component 声明错误都可能引发此错误——即,甚至是@Component({});——(注意分号)。

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

    从 2.0.0-rc.6 开始为真。

    【讨论】:

    • 感谢您的回答。它就像一个魅力。我正在使用 resharper,该工具用错误的分号误导了我。我花了几个小时才在这里找到解决方案,其中有一个小错误的半色,但在运行时抛出一个异常,并且没有任何线索提示。
    • "如果您收到此错误,请确保检查您的语法是否正确。" ...或者您实际上包含了 @Component 本身!哈哈。哎呀。谢谢@nikk Wong!
    • 当我错过了组件装饰器开头的@时,发生了这种情况。
    【解决方案3】:

    当我遇到它时,我会检查它很长时间。最后,在@Component({}) 的末尾,还有一个';'。不详

    【讨论】:

      【解决方案4】:

      在将根文件夹中的一些组件添加到单独的文件夹中后,我遇到了这个错误。问题在于使用 ./filename 作为 templateUrl 和 styleUrls 的路径,删除 ./ 后错误消失了。

      【讨论】:

        【解决方案5】:

        在我的例子中,我已经在组件类定义的上方声明了一个类,如下所示。我已将类 MyClass 定义移到导出类下方并且它起作用了。 所以我假设导出类必须是组件文件中的第一个声明。

        class MyClass {
        
        }
        export class MyComponent implements OnInit {
        

        【讨论】:

          【解决方案6】:

          如果您在同一文件上的应用组件声明之前有 @Injectable() 类,也可能存在此错误。

          【讨论】:

            猜你喜欢
            • 2016-12-24
            • 1970-01-01
            • 2018-04-01
            • 1970-01-01
            • 1970-01-01
            • 2017-04-16
            • 2018-11-26
            • 2017-10-03
            • 1970-01-01
            相关资源
            最近更新 更多