ReferenceError:未定义DashboardComponent

时间:2016-05-16 12:50:13

标签: angular angular2-routing

Angular2教程学习时我收到此错误。我检查了一切,但没有发现错误。这是我的代码

app.component.ts

import { Component } from '@angular/core';
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router-deprecated';
import { HeroesComponent } from './heroes.component';
import { HeroService } from './hero.service';


@Component({
    selector:"my-app",
    template:`
        <h1>{{title}}</h1>
        <nav>           
            <a [routerLink]="['Heroes']">Heroes</a>         
        </nav>
        <router-outlet></router-outlet>
    `,
    directives: [ROUTER_DIRECTIVES],
    providers:[
        ROUTER_PROVIDERS,
        HeroService
    ]
})

@RouteConfig([                      
        {
            path: '/dashboard',
            name: 'Dashboard',
            component:DashboardComponent,
            useAsDefault:true
        },
        {
            path:'/heroes',
            name:'Heroes',
            component:HeroesComponent
        }
])

export class AppComponent { 
    title = "My Heroes App";
}

dasboard.component.ts     从'@ angular / core'导入{Component};

@Component({
    selector: 'my-home',
    template: '<h3>My Dashboard</h3>'
})
export class DashboardComponent {}

当我从@RouteConfig

中删除以下代码块时,一切正常
{
    path: '/dashboard',
    name: 'Dashboard',
    component:DashboardComponent,
    useAsDefault:true
},

2 个答案:

答案 0 :(得分:2)

您还需要导入DashboardComponent组件:

import { DashboardComponent } from './dashboard.component';

我只看到导入的HeroesComponent ...

答案 1 :(得分:0)

我在&#34;英雄之旅&#34;中遇到了同样的错误信息。教程。教程说

/api/login

好的,我知道如何导入组件:

/api/protected

但它需要导入哪里?上面的答案是将其导入Also import and add DashboardComponent to our root NgModule's declarations.。恕我直言,这是不正确的。导入import { DashboardComponent } from './dashboard.component';的唯一模块是

app.component.ts

相反,app.component.ts需要导入两个文件:import { Component } from '@angular/core';DashboardComponent