2017-02-05 3 views
0

私はangular2アプリケーションに問題があります。独自のコンポーネントとセレクタを作成するとエラーが発生します

// app.component.ts 
import {Component} from '@angular/core'; 
import {CoursesComponent} from './courses.component' 

@Component({ 
    selector: 'my-app', 
    template: `<h1>Hello</h1><my-courses></my-courses>`, 
    directives: [CoursesComponent] 
}) 
export class AppComponent { 
    name = 'Angular2'; 
} 

// courses.component.ts 
import {Component} from '@angular/core'; 

@Component({ 
    selector: 'my-courses', 
    template: '<h2>Courses</h2>' 
}) 
export class CoursesComponent { 

} 

そして、私のアプリが動作しません:私は、そのコードを持っています。 app.component.tsのテンプレートから <my-courses></my-courses>を削除した後、それは動作し始めましたが、私のコースコンポーネントはなくなりました。 これを追加すると、エラーが発生します: enter image description here

私はそれに間違っていますか?

答えて

1

コンポーネントの一部は同じですかmodulemodulecomponentをインポート/宣言していないと思います。

はこれを行います。

import { CoursesComponent } from './your-path'; 
// ... other imports  

@NgModule({ 
    bootstrap: [ AppComponent ], 
    declarations: [ 
     AppComponent, 
     CoursesComponent 
     ... 
    ], 
    // ... 

export class AppModule { ... } 

はまた、あなただけAppModuleで、AppComponentCoursesComponentをインポートする必要はありません。

0

ちょうどあなたのngModuleにこれを追加します。

schemas: [ CUSTOM_ELEMENTS_SCHEMA ] 

インポートCUSTOM_ELEMENTS_SCHEMA@angular/core

から、あなたのコンポーネントセレクタに-を持っているためです。

関連する問題