gpt4 book ai didi

angular - 在 angular 2 组件中使用 rxjs,我应该手动导入运算符吗?

转载 作者:太空狗 更新时间:2023-10-29 18:14:26 24 4
gpt4 key购买 nike

http 的文档有 this example :

import {Http, HTTP_PROVIDERS} from 'angular2/http';

@Component({
selector: 'http-app',
viewProviders: [HTTP_PROVIDERS],
templateUrl: 'people.html'
})

class PeopleComponent {
constructor(http: Http) {
http.get('people.son')]
.map(res => res.json())
.subscribe(people => this.people = people);
}
}

但是,我需要添加这一行:import 'rxjs/add/operator/map 以使其工作。

我的配置是否不同或示例中缺少导入?

最佳答案

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 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.

例如,如您所述,显式添加 map :

import 'rxjs/add/operator/map';

或者,如果我们懒惰,我们可以直接引入全套运算符:

import 'rxjs/Rx';

关于angular - 在 angular 2 组件中使用 rxjs,我应该手动导入运算符吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35467948/

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