gpt4 book ai didi

angular - Rxjs 5 : share is not a function

转载 作者:行者123 更新时间:2023-12-01 10:26:34 26 4
gpt4 key购买 nike

更新到 RxJs 5 后,我收到以下错误:
share is not a function
代码:

import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';
import { Injectable } from '@angular/core';
import { share } from "rxjs/operators/share";
import * as m from '../../models/app.models';

@Injectable()
export class SharedService {
observable: Observable<any>;
observer: Observer<any>;

constructor() {
var temp = Observable.create((observer: Observer<any>) => {
this.observer = observer;
});
this.observable = temp.share(); <- Error here
}

broadcast(event: m.SharedEventModel) {
this.observer.next(event);
}

on(eventName, callback) {
return this.observable.filter((event) => {
return event.Name === eventName;
}).subscribe(callback);
}
}

尝试在 http://stackoverflow.com/a/36174340/3955513 之后创建 globalEvents

RxJs 版本:5.5.2

最佳答案

这是工作服务。

我已经从您用作引用的问题中更新了 Thierry Templier 的 plunker,Plunker

注意 import { share } from "rxjs/operators"; 不是 import { share } from "rxjs/operator/share"; ,如别处所述。见 lettable-operators.md


import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';
import { share } from "rxjs/operators"; // lettable operator, tree-shakeable
import { filter } from "rxjs/operators";

@Injectable()
export class SharedService {
observable: Observable<any>;
observer: Observer<any>;

constructor() {
var temp = Observable.create((observer: Observer<any>) => {
this.observer = observer;
});
this.observable = temp.pipe(share());
}

broadcast(event) {
this.observer.next(event);
}

on(eventName, callback) {
this.observable.pipe(filter(x => x.name === eventName))
.subscribe(callback);
}
}

关于angular - Rxjs 5 : share is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47221340/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com