从 Angular2 服务返回一个空的 Observable

2024-04-23

我正在构建一个 Angular 2 应用程序,它使用服务来收集数据。 该服务确实包含以下逻辑:

/* ========== 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() { 

    }

我的组件的构造函数没有被事件调用。 这该如何解决呢。

亲切的问候,


Replace Observable.empty() with Observable.of([])。的偏好of over empty是官方的 Angular英雄之旅 https://angular.io/docs/ts/latest/tutorial/toh-pt6.html教程用于返回一个空的可观察值,这是我在所有应用程序中使用的。

它们之间的区别似乎是是否不返回任何内容,或者返回一个空数组。

来自官方 rxjs 文档:

Observable.empty() http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-empty

创建一个 Observable,它不向观察者发送任何项目,并立即发出完整的通知。

Observable.of() http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-of

创建一个 Observable,它立即一个接一个地发出您指定为参数的一些值,然后发出完整的通知。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Angular2 服务返回一个空的 Observable 的相关文章