Trying to understand RxJS imports

99封情书 提交于 2019-11-29 04:20:53

It's pretty simple because TypeScript by default looks into node_modules directory.

Importing the following:

import {Observable} from 'rxjs/Observable';

is resolved as node_modules/rxjs/Observable.d.ts which is enough to compile the code.

Similarly importing rxjs/add/operator/toPromise is resolved as node_modules/rxjs/add/operator/toPromise.ts. Btw you can use the --traceResolution compiler option to see what TypeScript path are tested.

When you have your compiled JS (eg. in commonjs format) you can run your app in node because it'll call require('rxjs/Observable') which will resolve to node_modules/rxjs/Observable.js. Then similarly with rxjs/add/operator/toPromise.

Be aware that the code structure of RxJS github page is different than the actual npm package. Basically, just the package.json and the src dir with compiled .js and .d.ts files are uploaded to the npm repository (the original .ts source files are in node_modules/rxjs/src but you never want to work directly with them).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!