2016-09-15 14 views
16

私はAngular 2 CLIを使用していて、コンポーネント "MyComponent"をng generate component MyComponentで作成しました。私の知る限りでは、私は@Componentデコレータの指示キーと値のペアにコンポーネントを追加する必要がありますが、typescriptですコンパイルはと言って、この時点で失敗:新しいコンポーネントをtypescriptでアングル2アプリケーションに追加できません

ERROR in [default] /Users/Philip/Desktop/Angular2/src/app/app.component.ts:8:2 
Argument of type '{ selector: string; template: any; styles: any[]; directives: typeof MyComponentComponent[]; }' is not assignable to parameter of type 'Component'. 
    Object literal may only specify known properties, and 'directives' does not exist in type 'Component'. 

は、このアプリのために私のコードです:

import { Component } from '@angular/core'; 
import { MyComponentComponent } from './my-component/my-component.component' 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'], 
    directives: [MyComponentComponent], 
}) 
export class AppComponent { 
    title = 'app works!'; 
} 

私は、生成されたコンポーネントのコードには触れなかったが、念のため:

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

@Component({ 
    selector: 'app-my-component', 
    templateUrl: './my-component.component.html', 
    styleUrls: ['./my-component.component.css'] 
}) 

export class MyComponentComponent implements OnInit { 

    constructor() { 

    } 

    ngOnInit() { 
    } 

} 
+0

あなたはangular2を使用していますか? – micronyks

+0

使用している角バージョンと一致するバージョンのCLIを使用していますか?コンポーネントレベルの 'ディレクティブ'は廃止され、削除されました(RC6、IIRC)。 – jonrsharpe

+0

ドキュメントに示されているようにnpmコマンドを使用して両方を今日インストールしました。両方とも最新でなければならない、私はそれらが一致するかどうかわからない。 –

答えて

18

エラー自体は、ディレクティブがやると言っていますコンポーネントには存在しません。廃止予定です。以下のようにこのコードを試してみてください、

import { MyComponentComponent } from './my-component/my-component.component' 
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core'; 

@NgModule({ 
    ... 
    ... 
    declarations:[AppComponent,MyComponentComponent], //<---need to declare 
    schemas:  [CUSTOM_ELEMENTS_SCHEMA]    //<---added this line 
}) 

そしてディレクティブを削除:[MyComponentComponent] AppComponentから。

+0

ありがとうございます。 CLIで自動的にあなたの投稿に表示されるように、私はすでにapp.module.tsに宣言しています。 typescriptはコンパイルされますが、ブラウザでは実行されません。それは巨大なエラーです、私は元の投稿に更新しました。 –

+0

その後、 '@ Component'から'ディレクティブ:[...] '部分を削除しましたか?ただそれを削除します。私はそれが働くことを願っています... – micronyks

+0

私は確信していませんが、私はスキーマで私の答えを更新しました。それが機能するかどうか今すぐ確認してください。 – micronyks

34

Angular2コンポーネントデコレータは、他のコンポーネントの埋め込みにこれらを使用しなくなりました。代わりに、entryComponentsという新しいメタデータを使用する必要があります。あなたが角度CLIを使用した場合、新しいコンポーネントは自動的にインポートして、コメントを追加します

@Component({ 
    selector: 'app-vehicle-list', 
    templateUrl: './vehicle-list.component.html', 
    styleUrls: ['./vehicle-list.component.css'], 
    providers: [VehicleService] 
}) 
+2

"directives"を "entryComponents"に置き換えて動作させました。 ここをクリックしてください: https://angular.io/docs/ts/latest/api/core/index/Component-decorator.html – wojjas

+1

同じ問題です。この回答は役に立ちます。誰かがなぜ名前が変わるのか説明できますか? –

+0

また、@@ NgModuleでコンポーネントを宣言キーに置いて参照し、ファイルの先頭にインポートします(私の場合、@@ NgModuleはapp.module.tsにあります)。 –

1

...車両リストコンポーネントは、以下のコンポーネントメタデータを持っている...例として、以下の

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'], 
    entryComponents:[VehicleListComponent] 
}) 

を参照してください。 app.module.ts内の@ngmoduleの宣言セクションに移動します。そのためにコードを書く必要はありません。

0

次の2つのオプションがあります。

1:コンポーネント

2の内部使用entryComponentsタグ:それを自動的にインポート使用角度CLIを、私たちは、明示的にそのためのコードを記述する必要はありません。

1

コンポーネントの作成コマンドを実行した後、ANGULAR 4以上で何もする必要はありません。このエラーは、新しいコンポーネントのメタデータファイル(componentname.component.tsなど)の右のセレクタ名を使用していない可能性が最も高いです。

関連する問題