我应该同时使用`import'rxjs/Rx'`和`import {Observable} from'@ rxjs/Observable'两者吗? [英] Should I use both `import 'rxjs/Rx'` and `import { Observable } from '@rxjs/Observable'`

查看:531
本文介绍了我应该同时使用`import'rxjs/Rx'`和`import {Observable} from'@ rxjs/Observable'两者吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import { Observable } from '@rxjs/Observable';
import 'rxjs/Rx';
import 'rxjs/add/observable/throw';
@Component({});
export shellModule{}

这是我从某个地方复制的Angular应用程序的一部分代码(我已经删除了导出模块中的定义.我正在使用它来提供服务来调用API.

This is a piece of code form my Angular app that I copied from somewhere (I have removed the definitions in the exported module. I am using it to make a service to call APIs.

在此特定文件的导入中,为什么即使已导入整个 rxjs ,也要分别导入Observable.如果要完全导入特定模块,为什么要从该模块中单独导入特定对象?我尝试从提出问题的论坛上问这个问题,但没有答案.我想了解一下这是否有助于优化代码.

In the imports in this particular file, why is it that Observable is imported separately even though the entire rxjshas been imported. If a particular module is being imported in its entirety, why is a particular object from it imported separately? I tried asking this question at the forum from where I took it, but there was no answer. I want to understand if this somehow helps with optimization of code.

推荐答案

一般:

在Typescript中,模块的处理方式将要求您使用import * as rx from 'rxjs/Rx'加载整个库,或者使用库中的特定导出模块加载该库,以便编译器加载类型.

In Typescript, the way modules are handled would require you to either load in the entire library with the import * as rx from 'rxjs/Rx', or a specific exported module within the library to use it, so the the compiler loads in the types.

将导入减少到仅需要的特定模块,即可设置应用程序以使用 Angular的AOT编译.这不是由打字稿编译器完成的,而是由称为汇总工具的工具完成的.因此,它可以在以后帮助优化代码,但它不会自动这样做.

Reducing your imports to only the specific modules you need sets up your app to use tree shaking from Angular's AOT compilation. This is not done by the typescript compiler, but by a tool called rollup. So, it can help with optimizing code later, but it doesn't automatically do so.

就编译开销而言,引入整个库可能会稍微降低编译器的速度……但这不是一个很强的优点,除了非常复杂的库之外.

As far as compilation overhead, bringing in the whole library might slow down the compiler a bit... but this isn't a very strong point except for massively complex libraries.

我个人更喜欢导入特定模块,因为这样可以使调用代码更简洁一些,因为我不需要使用该全局名称即可获得特定名称. rx.ObservableObservable. lodash库就是一个很好的例子(rxjs有点复杂...)

I, personally, prefer importing in specific modules because it makes the calling code a little cleaner since I don't need to use that global name to get to the specific name. rx.Observable vs Observable. A good example of this is the lodash library (rxjs is a bit more complex...)

老实说,像您在那儿一样导入整个库:import 'rxjs/Rx'对我来说没有意义.您应该只导入特定的导出模块.尝试将其删除,查看出现什么错误,然后使用* as rx语法.

Honestly, importing entire libraries like the line you have there: import 'rxjs/Rx' doesn't make sense to me. You should only import specific exported modules. Try removing it, seeing what errors you get, and then using the * as rx syntax instead.

就rxjs而言-当您要导入特定运算符(如所引用问题的答案中所述进行设置.

As far as rxjs goes - it is a little wonky when you want to import specific operators like this question does - so the way to get specific operators is with: import 'rxjs/add/observable/from' - but that also requires a tinkering with your webpack set up as outlined in the referenced question's answer.

这篇关于我应该同时使用`import'rxjs/Rx'`和`import {Observable} from'@ rxjs/Observable'两者吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆