Angular 财产';出版';类型Observable上不存在<;字符串>;角度6
我正在Angular 财产';出版';类型Observable上不存在<;字符串>;角度6,angular,rxjs,angular6,Angular,Rxjs,Angular6,我正在Angular 6中编写我的第一个Angular应用程序 我正在实现错误处理以在一个地方处理所有错误,为此,我遵循 根据指南代码示例,我的通知服务如下 import {Injectable} from '@angular/core'; import {BehaviorSubject, Observable} from 'rxjs'; import {publish} from 'rxjs/operators'; @Injectable() export class Notificati
Angular 6
中编写我的第一个Angular
应用程序
我正在实现错误处理以在一个地方处理所有错误,为此,我遵循
根据指南代码示例,我的通知服务如下
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);
}
}
从'@angular/core'导入{Injectable};
从“rxjs”导入{BehaviorSubject,Observable};
从“rxjs/operators”导入{publish};
@可注射()
出口类通知服务{
私有通知:BehaviorSubject=新的BehaviorSubject(null);
只读通知$:Observable=此。_notification.asObservable().publish().refCount();
构造函数(){}
通知(信息){
此._通知。下一条(消息);
setTimeout(()=>this.\u notification.next(null),5000);
}
}
IDE在publish()处出现错误
我甚至从rxjs/operators
导入了publish
,但导入显示未使用。我还尝试了导入向导示例,但仍然得到相同的错误。可观察的上不再存在发布
功能,您需要将其与管道一起使用,如下所示:
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()
)
从'rxjs/operators'导入{publish,refCount};
从“rxjs”导入{BehaviorSubject,Observable};
//...
私有通知:BehaviorSubject=新的BehaviorSubject(null);
只读通知$:Observable=this.\u notification.asObservable().pipe(
publish(),
refCount()
)
作为publish
和refCount
的替代方案,您可能对执行这两种功能的share()
函数感兴趣。这是由于模式()的流行而引入的。如user184994所述,但稍作修改:
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(
share()
)
从'rxjs/operators'导入{publish,refCount};
从“rxjs”导入{BehaviorSubject,Observable};
//...
私有通知:BehaviorSubject=新的BehaviorSubject(null);
只读通知$:Observable=this.\u notification.asObservable().pipe(
股份()
)