Angular2 Http Request returns Observable without map 方法
Angular2 Http Request returns Observable without map method
我已经使用 HTTP_PROVIDERS
设置了我的应用程序
bootstrap(AppComponent, [
HTTP_PROVIDERS,
ROUTER_PROVIDERS,
provide(LocationStrategy, { useClass: HashLocationStrategy }),
PostService
]);
我的服务很像
@Injectable()
export class PostService {
posts = [];
constructor(http: Http) {
this.http = http;
}
}
但是当我打电话时
this.http.get('/posts')
它 returns 一个 Observable
没有 map
方法,它让我疯狂了几个小时。我正在使用 Babel 转译我的 javascript 代码。
新出炉的Server Communication开发指南(终于)discusses/mentions/explains这个:
The RxJS library is quite large. Size matters when we build a production application and deploy it to mobile devices. We should include only those features that we actually need.
Accordingly, Angular exposes a stripped down version of Observable
in the rxjs/Observable
module, a version that lacks almost all operators including the ones we'd like to use here such as the map
method...
It's up to us to add the operators we need. We could add each operator, one-by-one, until we had a custom Observable implementation tuned precisely to our requirements.
例如,如上@Langley 的评论所示:
import 'rxjs/add/operator/map';
或者,如果我们懒惰,我们可以直接引入全套运算符:
import 'rxjs/Rx';
我已经使用 HTTP_PROVIDERS
bootstrap(AppComponent, [
HTTP_PROVIDERS,
ROUTER_PROVIDERS,
provide(LocationStrategy, { useClass: HashLocationStrategy }),
PostService
]);
我的服务很像
@Injectable()
export class PostService {
posts = [];
constructor(http: Http) {
this.http = http;
}
}
但是当我打电话时
this.http.get('/posts')
它 returns 一个 Observable
没有 map
方法,它让我疯狂了几个小时。我正在使用 Babel 转译我的 javascript 代码。
新出炉的Server Communication开发指南(终于)discusses/mentions/explains这个:
The RxJS library is quite large. Size matters when we build a production application and deploy it to mobile devices. We should include only those features that we actually need.
Accordingly, Angular exposes a stripped down version of
Observable
in therxjs/Observable
module, a version that lacks almost all operators including the ones we'd like to use here such as themap
method...It's up to us to add the operators we need. We could add each operator, one-by-one, until we had a custom Observable implementation tuned precisely to our requirements.
例如,如上@Langley 的评论所示:
import 'rxjs/add/operator/map';
或者,如果我们懒惰,我们可以直接引入全套运算符:
import 'rxjs/Rx';