2017-09-28 12 views
0

サービスコンポーネントが値を保持していて、それをサブスクライブするコンポーネントが変更されたときに、サービスコンポーネント通信をアングルで実装しようとしています。Angular + rxjs:サブスクリプションのプロバイダがありません

import { Injectable } from '@angular/core'; 
import { Observable } from 'rxjs'; 
import { Subject } from 'rxjs/Subject'; 

@Injectable() 
export class PracticeContextService { 
    practice: Subject<any> = new Subject<any>(); 
    public practiceSelected$ = this.practice.asObservable(); 

setPractice(providerId: string) { 
    this.practice.next({ providerId: providerId }); 
} 

getPractice(): Observable<any> { 
    return this.practice.asObservable(); 
} 
} 

およびコンポーネントに:私はrxjsスクリプションを使用していますが、私はここで

Uncaught (in promise): Error: No provider for Subscription! 

を得ていることは、私はサービスでやっているものです

import { Component, OnDestroy, OnInit } from '@angular/core'; 
import { Subscription } from 'rxjs/Subscription'; 
import { PracticeContextService } from '../practice-context.service'; 

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

export class PracticeContextSelectorComponent implements OnInit, OnDestroy { 

    practice: string; 

    constructor(private context: PracticeContextService, 
      private subscription: Subscription) {} 

    ngOnInit() { 
    this.subscription = this.context.practiceSelected$.subscribe(
     practice => { 
     this.practice = practice; 
     }); 
    } 

    ngOnDestroy() { 
    this.subscription.unsubscribe(); 
    } 
} 

コンポーネントサービスはモジュールにバンドルされ、後で別のモジュールに注入されます。

import { NgModule }   from '@angular/core'; 
import { CommonModule }  from '@angular/common'; 

import { PracticeContextSelectorComponent } from './practice-context-selector/practice-context-selector.component'; 
import { PracticeContextService } from './practice-context.service'; 

@NgModule({ 
    imports : [ 
    CommonModule 
    ], 
    declarations : [ 
    PracticeContextSelectorComponent 
    ], 
    providers : [ 
    PracticeContextService, 
    ], 
    exports: [ 
    PracticeContextSelectorComponent 
    ] 
}) 

export class PracticeContextModule {} 

どうやら、私はあなたのサービスでは、ここで

+0

あなたはAngulars DIによってインスタンス化クラスにコンストラクタのパラメータを持っている場合、が存在しなければなりませんマッチングプロバイダ。コンストラクタ(プライベートコンテキスト:PracticeContextService、 プライベートサブスクリプション:サブスクリプション)のこのパラメータの目的は何ですか? –

+1

サブスクリプションはDI用ではないので、 'rxjs/Subscription'の 'import {Subscription} 'のようにインポートしてください。 –

+0

@ lyubimov-roman私は実際にコンポーネントでこのインポートを行っています。この依存関係をコンストラクタ内のコンポーネントに接続する必要がない場合、使用する正しい方法は何ですか?私はこのアプローチを利用していくつかの例を見つけたが、私のためにはうまくいかないと思っているからです。 – vitalym

答えて

1

何か間違ったことをやっている、あなたの可能性このようなすべての変更rxjs輸入の最初:私はすでに時に問題があった

import { Observable, Subscription } from 'rxjs/Rx'; 

{Observable}を 'rxjs/Observable'からインポートします。今私はこれに注意しています:P! (私は多分本当にシーンの背後に何が起こるかを調べるために、さらに掘る必要があります。)

次にを、すでに上記のコメントでLyubimovローマを言ったように、あなたは、コンストラクタでrxjs サブスクリプションをインポートするべきではありません。私はLyubimovのコメントに対するあなたの答えを理解していません。意図していることを理解できるように、あなたが言及した例を私たちに提供してもらえますか?

あなたはこのようにそれをインポートし、コンポーネントにサブスクリプションオブジェクトを使用する必要がある場合:

import { Subscription } from 'rxjs/Rx'; 
+0

モジュール全体をインポートしたくない場合は、それを別々にインポートする方がよいでしょう。 –

+0

実際には、 サブスクリプション:サブスクリプションなど、タイプを宣言する部分で宣言できることに気付きました。コンストラクタの代わりに ありがとう! – vitalym

関連する問題