我想使用RxJS-DOM库来解析文件中的json数据。在将这个库包含到Angular2应用程序时,我遇到了一些困难。首先,我用NPM安装了RxJS-DOM。我需要用import表达式将这个库导入到我的服务文件中。
import {name} from 'path/to'
例如,通过与import {Injectable} from 'angular2/core'类比,我如何知道使用哪个name和哪个path来导入这个库?
我需要像这样在index.html中包含这个库吗?为什么?

谢谢。
发布于 2016-06-13 23:18:57
通常是SystemJS,它会为您加载模块。您正在使用systemjs.config.js文件或index.html中的内联来配置它。在那里,您告诉它从您的node_modules文件夹加载包,以及您希望使用哪个名称注册它,等等。
如果您使用的是Angular 2快速入门项目和Angular 2的测试版,那么配置可能是在您的index.html中完成的,而且非常简单。您会发现,您想要使用的每个额外的NodeJS模块,都必须使用<script>标记进行导入。(如果您采用SystemJS config路线,情况并非如此)
所以当你导入你的TypeScript文件时,你的基本路径通常是node_modules文件夹。如果想要从node_modules文件夹导入不是模块的“本地”文件,可以使用
import {class} from './path/file';关于你的JSON问题--我会利用Angular的Http类来读入JSON文件,如下所示:
interface IConfig {
thingEnabled: boolean;
otherStuff: string;
}
[...]
this.http.get('./config.json')
.map((res: Response) => res.json())
.subscribe((res: IConfig) => {
// do something with the already deserialized JSON here
console.log(res.otherStuff);
}); https://stackoverflow.com/questions/37792638
复制相似问题