加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

从Angular2服务返回一个空的Observable

发布时间:2020-12-17 10:25:46 所属栏目:安全 来源:网络整理
导读:我正在构建一个Angular 2应用程序,它使用服务来收集数据. 该服务确实包含以下逻辑: /* ========== CORE COMPONENTS ========== */import { Injectable } from '@angular/core';import { Http,Response } from '@angular/http';import { Observable } from '
我正在构建一个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(在promise中):错误:序列中没有元素..

编辑:添加组件

@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() { 

    }

我的组件的构造函数不是调用的事件.
如何解决这个问题.

亲切的问候,

用Observable.of([])替换Observable.empty(). over empty for the empty是官方Angular Tour of Heroes教程用来返回空的observable,并且是我在所有应用程序中使用的内容.

它们之间的区别似乎是返回任何内容,还是空数组.

来自官方rxjs文档:

Observable.empty()

Creates an Observable that emits no items to the Observer and immediately emits a complete notification.

Observable.of()

Creates an Observable that emits some values you specify as arguments,immediately one after the other,and then emits a complete notification.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读