私はAngularを学習しようとしており、おそらく非常に単純な問題に遭遇しました。Angular2コントロールをプロバイダーとして組み込む必要があるのはなぜですか?
"PageSelectorComponent"というグリッドページングを行うための子コンポーネントを作成しました。このコンポーネントは合計レコード数を取得し、ページリンクを計算して表示します(このコンポーネントではプロバイダは使用されません)。私はこのコンポーネントを "TestComponent"と呼ばれる別のコンポーネントの中に埋め込んでいます。
PageSelectorComponentとTest Componentは、私のapp.module.tsファイルのdelarations部分に含まれています。私は除いてすべてが正常に動作しますapp.module.tsのプロバイダセクションにPageSelectorComponentを追加した場合、今、私は部品の2つのインスタンスを得る
ERROR Error: Uncaught (in promise): Error: No provider for PageSelectorComponent!
:
私の問題は、私は次のエラーを取得するということです。これは私のサブスクリプションが動作しない原因になります。
PageSelectorComponentとTestComponentがどのように宣言されているかを見てきましたが、違いは見られません。
なぜアプリケーションがPageSelectorComponentをプロバイダとしてリストアップしないと不平を言っているのですか?私が言ったように
@Component({
moduleId: module.id,
selector: 'app-page-selector',
templateUrl: './page-selector.component.html',
styleUrls: ['./page-selector.component.css'],
})
export class PageSelectorComponent {
// member variables declared here
.
.
private static instanceNum: number = 0; // TOOD: debugging
constructor() {
PageSelectorComponent.instanceNum += 1;
console.log(`page-selector instance number ${PageSelectorComponent.instanceNum}`)
this.subject = new Subject();
}
が、私はいくつかの本当に基本的な存在だと思います。ここでは
@NgModule({
declarations: [
AppComponent,
NavigationComponent,
HomeComponent,
PageSelectorComponent,
TestComponent,
],
imports: [
BrowserModule,
RoutingModule,
],
providers: [
PageSelectorComponent // App complains if I leave this out but now I get 2 instances of the component.
],
bootstrap: [AppComponent]
})
がPageSelectorComponentの定義である:ここで
は私app.modules.tsのNgMoudle部であり、私はここで逃した角の事。見た目の提案はありますか?
ありがとうございます!
あなたは 'providers'、' PageSelectorComponent.instanceNum + = 1; 'に置く必要はありません。ここで問題が起きるのは' this.instanceNum?'もし他のコンポーネントに' PageSelectorComponent.instanceNum + = 1; 'のようなものがあるなら、それをやるのをやめるのが良いでしょう。 – masterpreenz
クイック返信ありがとう。私はプロバイダセクションのPageSelectorComponentをリストする必要はありません。もう一度取り戻そうとしましたが、上記と同じエラーが発生します。 'static instanceNum'も取り出しました。私はまだ2つのインスタンスを取得しています。私の考えは、宣言**と**のプロバイダにリストされているためです。 –
「PageSelectorComponentのプロバイダがありません」と表示された場合、サービスのように 'PageSelectorComponent'が使用されたことを意味します。プロバイダは他のコンポーネントのどこかで動作します。 – masterpreenz