【发布时间】:2016-05-23 20:46:31
【问题描述】:
我正在尝试将 shared 运算符用于观察者,但它不起作用:
我的ts代码:
import { Injectable } from '@angular/core';
import { Observer } from 'rxjs/observer';
import { Observable } from 'rxjs/observable';
import 'rxjs/add/operator/share';
@Injectable()
export class LoadingService {
private _observer: Observer<string>;
loading$: Observable<string>;
constructor() {
this.loading$ = new Observable<string>(
observer => this._observer = observer).share();
}
toggleLoadingIndicator(name) {
if (this._observer) {
this._observer.next(name);
}
}
}
当我调用.share(); 时出现错误:TypeError: (intermediate value).share is not a function。
在其他情况下,我成功导入例如地图运算符,并正常使用如下:
import 'rxjs/add/operator/map';
this._http.get(this._url, {
headers: this._headers
}).map(r => r.json()).subscribe(json => {
console.log(json);
this.isValid = true;
});
因此,即使我尝试使用 map 运算符来证明导入,在第一种情况下 this.loading$ = new Observable<string>(observer => this._observer = observer).map(n => n); 我也会遇到相同的错误。
【问题讨论】:
-
您是从 TSC 还是从 WebStorm 等 IDE 收到此错误?如果来自 tsc,也许尝试更新到最新版本的打字稿。如果来自 WS,则 rxjs observables 的操作符导入存在一个已知问题,应在下一个补丁中解决:youtrack.jetbrains.com/issue/WEB-20992#u=1458951972769
-
在浏览器中,当我运行应用程序时
-
您使用的是什么运行时转译器和版本?
-
BroccoliTypeScriptCompiler,角度 rc1
标签: import angular rxjs observable