Rxjs 6的Observable.create(subscriber-> {...})。share()

时间:2018-06-19 13:02:14

标签: angular typescript rxjs observable rxjs6

我将Angular 5应用升级到Angular 6,因此从rxjs 5升级到rxjs 6,在迁移以下代码时遇到了麻烦:

const myObservable = Observable.create(subscriber => {
    // do something with the subscriber
}).share();

尤其是我遇到此错误:

  

TypeError:Observable_1.Observable.create(...)。share不是   functionTypeError:Observable_1.Observable.create(...)。share不是

2 个答案:

答案 0 :(得分:11)

您需要按以下方式传递share()而不是进行链接:

const myObservable = Observable.create(subscriber => {
    // do something with the subscriber
}).pipe(share());

还要确保您按以下方式导入共享:

import {share} from 'rxjs/operators';

答案 1 :(得分:1)

import { Observable } from "rxjs";
...
let obs$ = new Observable(...);
...

上面的代码应该可以解决问题