Anu*_*TBE 2 rxjs angular angular6
我正在写我的第一个Angular申请表Angular 6.
我正在实现错误处理来处理单个地方的所有错误,为此,我正在遵循此媒体指南
根据stackblitz上的指南代码示例,我的NotificationService就像
import {Injectable} from '@angular/core';
import {BehaviorSubject, Observable} from 'rxjs';
import {publish} from 'rxjs/operators';
@Injectable()
export class NotificationService {
private _notification: BehaviorSubject<string> = new BehaviorSubject(null);
readonly notification$: Observable<string> = this._notification.asObservable().publish().refCount();
constructor() {}
notify(message) {
this._notification.next(message);
setTimeout(() => this._notification.next(null), 5000);
}
}
Run Code Online (Sandbox Code Playgroud)
IDE正在给出错误 publish()
我甚publish至从rxjs/operators导入但导入显示未使用.我也尝试了导入示例中的导入,但仍然得到相同的错误.
该publish功能不再存在Observable,您需要将它与管道一起使用,如下所示:
import { publish, refCount } from 'rxjs/operators';
import { BehaviorSubject, Observable } from 'rxjs';
//...
private _notification: BehaviorSubject<string> = new BehaviorSubject(null);
readonly notification$: Observable<string> = this._notification.asObservable().pipe(
publish(),
refCount()
)
Run Code Online (Sandbox Code Playgroud)