2016-06-20 29 views
8

我正在尝试导入D3,以便我可以在Angular2模块中使用它。Angular2-Seed + Typings + D3:导入错误,'找不到模块'd3''

一些背景资料:

我所做的:

  1. 我安装了NPM D3包:

    npm install d3 --save 
    
  2. 我安装使用分型的D3型的描述,作为是Angular2-Seed为它已经安装的库所使用的。

    typings install d3 --save 
    
  3. 然后,在我的Angular2模块文件,我添加了import语句

    import * as d3 from 'd3'; 
    

结果是TSC是给我的错误信息“无法找到模块 'D3' “。我错过了什么或做错了什么?

+0

你能否尝试从[这里]下载软件包(https://www.npmjs。com/package/ng2-nvd3) –

+0

我安装了它,设置了discreteBarChart演示程序,但它在浏览器中完全不显示,尽管没有浏览器错误,没有编译器错误。但是在我将更多的时间投入NG2-NVD3工作之前,我最好停下来:由于我需要构建高度自定义的可视化,NVD3对我来说不会有多大用处。 – Devaro

回答

3

因此,如果的package.json您已经有喜欢的依赖性:

"dependencies": { 
    ... 
    "d3": "^3.5.17", 
    ... 
} 

然后你可以在/tools/config/seed.config.ts,并添加去 'D3': '$ {this.NPM_BASE} D3/d3.min.js'SYSTEM_CONFIG_DEV对象,如:

protected SYSTEM_CONFIG_DEV: any = { 
    defaultJSExtensions: true, 
    packageConfigPaths: [ 
     `${this.NPM_BASE}*/package.json`, 
     `${this.NPM_BASE}**/package.json`, 
     `${this.NPM_BASE}@angular/*/package.json` 
    ], 
    paths: { 
     [this.BOOTSTRAP_MODULE]: `${this.APP_BASE}${this.BOOTSTRAP_MODULE}`, 
     '@angular/core': `${this.NPM_BASE}@angular/core/bundles/core.umd.js`, 
     '@angular/common': `${this.NPM_BASE}@angular/common/bundles/common.umd.js`, 
     '@angular/compiler': `${this.NPM_BASE}@angular/compiler/bundles/compiler.umd.js`, 
     '@angular/forms': `${this.NPM_BASE}@angular/forms/bundles/forms.umd.js`, 
     '@angular/http': `${this.NPM_BASE}@angular/http/bundles/http.umd.js`, 
     '@angular/router': `${this.NPM_BASE}@angular/router/index.js`, 
     '@angular/platform-browser': `${this.NPM_BASE}@angular/platform-browser/bundles/platform-browser.umd.js`, 
     '@angular/platform-browser-dynamic': `${this.NPM_BASE}@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js`, 
     'rxjs/*': `${this.NPM_BASE}rxjs/*`, 
     'd3': '${this.NPM_BASE}d3/d3.min.js', 
     'app/*': `/app/*`, 
     '*': `${this.NPM_BASE}*` 
    }, 
    packages: { 
     rxjs: { defaultExtension: false } 
    } 

让我知道它是否有帮助。谢谢!

+0

谢谢,这正是缺少的。在我使用[Angular2-Seed](https://github.com/mgechev/angular2-seed)之前,编辑System.config()的路径和/或包变量是添加大多数库的常见步骤, t学会了如何在这个种子中做同样的事情。 – Devaro

+0

随着最终Angular2的第一次发布,systemjs.js的添加必须是安装了d3.js的文件夹。为了说清楚,您必须指出您下载d3的位置,我的位置在与设置文件相同的文件夹,位于d3的子文件夹中,因此该行看起来像这样。 'd3':'./d3/d3.min.js' 在我的情况下,这是map:object中的最后一行。 感谢您的澄清。 –

1

我有同样的问题,上面的答案有助于调试我的soltution - 因为它确定它是一个配置问题,但使用[email protected]我不得不更新{root} /e2e/tscconfig.json (加入:

 "types": [ 
     "d3" 
    ] 

如下:

{ 
    "compileOnSave": false, 
    "compilerOptions": { 
    "declaration": false, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "module": "commonjs", 
    "moduleResolution": "node", 
    "outDir": "../dist/out-tsc-e2e", 
    "sourceMap": true, 
    "target": "es5", 
    "typeRoots": [ 
     "../node_modules/@types" 
    ], 
    "types": [ 
     "d3" 
    ] 
    } 
} 

请记住,存在{}根一tscconfig.json/src目录/还有我在这个更新,我仍然有。依赖性问题:

import * as D3 from 'd3'; 

在我的组件中通过这个错误。希望这可以帮助至少一个人!

+0

Thanx为我工作 –

+0

谢谢你的投票。我打开了一个RFC的类型更新他们的类型D3,我收到了答复,并提交了我的更改,他们接受 - 但我从来没有建立他们所需的测试文件....所以我不知道它是否更新。 –