2017-01-20 7 views
1

私は角度2.3を使用しています。そして、はい、私は半ダースかそれ以外の同様の質問を見ましたが、私の問題を解決するための答えは見つけられませんでした。はコンポーネントの既知のプロパティではないため、入力varにバインドできません

私はLabelSpanComponentと呼ばれるコンポーネントがあります。

// label-span.ts 
export interface LabelSpan { 
    labelValue: string, 
    spanValue: string 
} 

そして、私はページ上でそれを使用する場合、これはうまく働いた:

// label-span.component.ts 
import {Component, Input} from "@angular/core"; 
import {LabelSpan} from "../models/label-span"; 

@Component({ 
    selector: 'label-span', 
    template: ` 
     <label *ngIf="entry.labelValue">{{entry.labelValue}} </label> 
     <span>{{entry.spanValue}}</span>` 
}) 
export class LabelSpanComponent { 
    @Input() entry: LabelSpan 
} 

をLabelSpanです。私は、2つの異なるページで独自のモジュールとそれぞれを、それを使用しようとしたときしかし、私は角度からのエラーを得た: ThisDetailModuleとThatDetailModule:

タイプLabelSpanComponent 2つのモジュールの宣言の一部です! LabelSpanComponentを ThisDetailModuleとThatDetailModuleをインポートする上位モジュールに移動することを検討してください。また、LabelSpanComponentをエクスポートして含む 新しいNgModuleを作成し、NgModuleをThisDetailModuleおよびThatDetailModuleにインポートして をインポートすることもできます。だから、

、私はおそらくLabelSpanComponentためのモジュールを作成する必要があることを決めた:

// label-span.module.ts 
import {NgModule} from "@angular/core"; 
import {FormsModule} from "@angular/forms"; 
import {BrowserModule} from "@angular/platform-browser"; 
import {LabelSpanComponent} from "./label-span.component"; 

@NgModule({ 
    imports: [ 
     FormsModule, 
     BrowserModule 
    ], 
    declarations: [ 
     LabelSpanComponent 
    ], 
    providers: [ ] 
}) 
export class LabelSpanModule { } 

そして私はapp.module.tsにこのモジュールを追加しました:両方ThisDetailModuleで

// app.module.ts 
/* other imports */ 
import {LabelSpanModule} from "./templates/label-span.module"; 

@NgModule({ 
    imports: [ 
     /* other modules */ 
     LabelSpanModule 
    ], 
    declarations: [ AppComponent ], 
... 
}) 
export class AppModule{ } 

とThatDetailModule、私はLabelSpanModuleをインポートの一部として含めました。ただし、次のエラーが発生しています。

「ラベル - スパン」の既知のプロパティではないため、「エントリ」にバインドできません。 1. 'label-span'がAngularコンポーネントで、 'entry'入力がある場合は、それがこのモジュールの一部であることを確認します。

ああ、それは価値があるもののために、ここでは、HTMLページで利用されている方法です。

ページ用のコンポーネントファイルで、私が持っている
<label-span id="the-id" [entry]="myVar"></label-span> 

myVarに=を{ labelValue: "ラベル"、spanValue: "The Span"}

私は何が間違っているのでしょうか?あなたがコンポーネント

@NgModule({ 
    imports: [ 
     FormsModule, 
     BrowserModule 
    ], 
    declarations: [ 
     LabelSpanComponent 
    ], 
    exports: [LabelSpanComponent] 
    providers: [ ] 
}) 
export class LabelSpanModule { } 

をエクスポートする必要が

+0

あなたのコンポーネント用に別のモジュールを必要としません。しかし、そのモジュールがどこに属していても、それを宛先モジュールにインポートする必要があります。また、コンポーネントを含むモジュールの「宣言」セクションにそのコンポーネントを記述する必要があります。 –

+0

プロジェクトからlabel-span.module.tsを削除し、app.module.tsの宣言にLabelSpanComponentを追加すると、「エントリ」へのバインディングについて同じエラーメッセージが表示されます – Machtyn

答えて

3

は便利な情報のためのドキュメントの共有モジュールのセクションを確認してください:https://angular.io/docs/ts/latest/guide/ngmodule.html#!#shared-module

+0

時には、あなたが見逃した簡単なことです。ありがとう! – Machtyn

+0

@Machtynは私たちにとって最高のところです:-) – echonax

+1

また、App.moduleを除くすべてのモジュールから 'BrowserModule'を削除してください。サブモジュールの場合は、 'commomModule'だけを使用してください。 https://angular.io/docs/ts/latest/cookbook/ngmodule-faq.html#!##-browser-vs-common-moduleをご覧ください。 – vinagreti

関連する問題