我已使用 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')
它返回 Observable
其中没有 map
方法,它让我发疯了好几个小时。我使用 Babel 来转译我的 javascript 代码。
最佳答案
新类型转换的Server Communication开发指南(最后)讨论/提到/解释了这一点:
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';
关于javascript - Angular2 Http请求返回Observable而不带map方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35025547/