2017-03-07 7 views
4

私はサービスを使ってデータを収集するAngular 2アプリケーションを構築しています。 サービスは、次のロジックが含まれていない:Angular2サービスから空のObservableを返します。

/* ========== CORE COMPONENTS ========== */ 
import { Injectable } from '@angular/core'; 
import { Http, Response } from '@angular/http'; 
import { Observable } from 'rxjs/Observable'; 
import 'rxjs/Rx'; 
import 'rxjs/add/operator/map'; 
import 'rxjs/add/operator/catch'; 

/* ========== MODELS ========== */ 
import { IApplicationViewModel } from './models/application.model'; 

/* ========== CONSTANTS ========== */ 
import { LogzillaServerConfiguration } from '../../app.configuration'; 

@Injectable() 
export class ApplicationService { 
    private getAllEndpoint = LogzillaServerConfiguration.host + '/api/administration/application/getAll'; 

    // Initializes a new instance of the 'ApplicationService'. 
    constructor(private http: Http) { } 

    // Get all the 'logzilla' applications. 
    getApplications(): Observable<IApplicationViewModel[]> { 
     return this.http.get(this.getAllEndpoint) 
      .map((response: Response) => <IApplicationViewModel[]>response.json().model) 
      .catch(this.handleError); 
    } 

    // Handle an error that occured while retrieving the data. 
    // This method will throw an error to the calling code. 
    private handleError(error: Response) { 
     return Observable.empty(); 
    } 
} 

何このサービスは、エンドポイントからデータを取得し、エラーが発生したときに、私は空のObservableを返すよ、モデルにマップされません。

また、アクティブルートが変更される前に、サービスからデータをロードするために使用されるリゾルバもあります。 このリゾルバは、以下の論理含まれていない:

/* ========== CORE COMPONENTS ========== */ 
import { Injectable } from '@angular/core'; 
import { Resolve, ActivatedRouteSnapshot } from '@angular/router'; 

/* ========== APPLICATION SERVICES ========== */ 
import { ApplicationService } from './application.service'; 

/* ========== MODELS ========== */ 
import { IApplicationViewModel } from './models/application.model'; 
import { IApplicationLogLevel } from './models/applicationLevel.model'; 

// Defines the 'resolver' which is used to resolve the applications. 
@Injectable() 
export class ApplicationResolver implements Resolve<IApplicationViewModel[]> { 

    // Initializes a new instance of the 'ApplicationResolver'. 
constructor(private applicationService: ApplicationService) { } 

// Get all the registered applications. 
resolve(route: ActivatedRouteSnapshot) { 
    return this.applicationService.getApplications(); 
} 
} 

私のルートの定義は次のとおりです。ただし

{ path: 'applications', component: ApplicationComponent, pathMatch: 'full', resolve: { 
      application: ApplicationResolver 
     }}, 

、私は/applicationsを参照するとき、私はエラーUncaught (in promise): Error: no elements in sequence.で迎えていますが。

編集:追加コンポーネント

@Component({ 
    selector: 'logzilla-applications', 
    templateUrl: './src/app/pages/applications/application.template.html' 
}) 
@Injectable() 
export class ApplicationComponent implements OnInit { 
    hasApplications: boolean; 
    applications: IApplicationViewModel[]; 
    errorMessage: string; 

    // Initializes a new instance of the 'ApplicationComponent'. 
    constructor (private route: ActivatedRoute) { 
     console.log('I am here.'); 
    } 

    // This method is executed when the component is loaded. 
    ngOnInit() { 

    } 

私のコンポーネントのコンストラクタが呼び出されたイベントではありません。 これをどのように解決できますか。

種類について、

+1

は、我々が見ることができるとあなたは 'Observable.emptyを(置き換えるとどう' ApplicationComponent' – Smit

+0

) '' Observable.of([]) ' –

+0

私が編集されたコンポーネントを追加しましたが、それはほとんど空ですが、だから私はあなたがそれを必要とする理由は分かりません:) – Complexity

答えて

5

Observable.empty()Observable.of([])に置き換えます。 ofemptyの関係は公式の角度Tour of Heroesのチュートリアルで空の観測可能性を返すために使用されているものであり、すべてのアプリで使用しています。

これらの違いは、何も返されないか、空の配列のようです。公式rxjsドキュメントから

Observable.empty()

は、観察者にはアイテムを発していないと、すぐに完了通知を発すること観測を作成します。

Observable.of()

あなたはすぐに、引数として次々に指定し、[完了通知を発するいくつかの値を放射する観測を作成します。

+0

ありがとうございます。どのように私のコンポーネントが今それが検索に失敗したか、またはデータが返されなかった場合、どのような方法がありますか?検索に失敗したときに何らかのエラーメッセージが表示される可能性があるためです。 – Complexity

+0

[Observable.throw'](http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-throw)とこの質問[how-to -catch-exception-from-http-request](http://stackoverflow.com/questions/35326689/how-to-catch-exception-correctly-from-http-request) –

+0

これを実装すると、エラーがポップアップを開始し、リゾルバで例外が発生したことを示します。 – Complexity

3

リゾルバは、所与の観測を購読及び成分(実際ActivatedRouteオブジェクト)に観測シーケンスの最初の結果を通過しようとします。

しかし、結果が出される前にObservableが完了したイベントを起動するため、リゾルバは結果を取得しません。だからあなたのエラーがどこから来るのですか。

Observable.of(null)またはObservable.of([])などのいずれかを返す必要があります。 次に、コンポーネントの空の入力を処理できます。

Thisページには、Observable.empty()機能の詳細が含まれています。

+1

Observable.of(null)は、観測可能な型のパラメータを定義した場合の答えです(例:Observable ) – jcroll

関連する問題