Jan*_*nka 4 error-handling rxjs angular2-services
我有一个有角度的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;
}
}
Run Code Online (Sandbox Code Playgroud)
该服务结合了提出异常的方法.当我尝试订阅
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);
});
}
Run Code Online (Sandbox Code Playgroud)
我没有收到错误.如何在ReplaySubject中引发异常?
reply.error("some error"); 应该这样做.
但是,我不建议你抛出一个错误ReplaySubject- 因为任何错误都会最终确定Subject并使其无法用于将来的使用,并会自动取消订阅任何订阅者 - 除非这是你想要在这里实现的.
| 归档时间: |
|
| 查看次数: |
858 次 |
| 最近记录: |