我正在構建一個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() {
}
我的組件的構造是不叫事件。 這怎麼解決。
此致
我們可以看到'ApplicationComponent' – Smit
如果更換'Observable.empty(會發生什麼)'和'Observable.of([])' –
我添加的組件作爲一個編輯,但它幾乎是空的,所以我不明白爲什麼你需要它:)à – Complexity