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

错误处理 – RxJs如何在ReplaySubject中引发异常?

发布时间:2020-12-17 17:33:08 所属栏目:安全 来源:网络整理
导读:我有一个有角度的2服务 import * as localforage from "localforage";import { ReplaySubject } from 'rxjs/ReplaySubject';@Injectable()export class CommentService { private localForage = require("localforage"); addComment (myvalue: string): Obse
我有一个有角度的2服务

import * as localforage from "localforage";
import { ReplaySubject } from 'rxjs/ReplaySubject';

@Injectable()
export class CommentService {
    private localForage = require("localforage");

    addComment (myvalue: string): Observable<Comment[]> {
        var reply:ReplaySubject<any> = new ReplaySubject(1);
        localforage.setItem(that.key,that.elencoCommenti).then(function (value) {
            //throw new Error("Value cannot be 3");
            reply.throw(Error('Error2'));           
            //              reply.next( value );
            //              reply.complete();
        });
        return reply;
    }

}

该服务结合了提出异常的方法.
当我尝试订阅

submitComment(){
    // Variable to hold a reference of addComment
    let commentOperation:Observable<string>;

    commentOperation = this.commentService.addComment(this.model)

    // Subscribe to observable
    commentOperation.subscribe(
                            comments => {
                                console.log('ok:');
                                console.log(comments);
                            },err => {
                                // Log errors if any
                                console.log('error:');
                                console.log(err);
                            });
}

我没有收到错误.
如何在ReplaySubject中引发异常?

解决方法

reply.error(“一些错误”);应该这样做.

但是我不建议你在ReplaySubject中抛出错误 – 因为任何错误都会最终确定主题并使其无法用于将来的任何用户并自动取消订阅任何订阅者 – 除非这是你想要在这里实现的.

(编辑:李大同)

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

    推荐文章
      热点阅读