在Angular 2中导入D3.js.

时间:2017-08-03 17:18:01

标签: angular d3.js

我刚从php转到Angular 2,没有太多的javascript或打字稿经验。因此,我需要你的帮助填补空白。

我正在开发一个相当大的应用,我需要使用D3.js.我的开发应用程序建立在官方快速入门(https://github.com/angular/quickstart)之上。

我尝试导入d3-ng2-service(https://www.npmjs.com/package/d3-ng2-service),据我读到SO和github,代码是正确的。然而,它不起作用。这里有一些代码,为了简洁而编辑。

src / app / app.module.ts:

mvn install

的src / systemjs.config.js

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }   from '@angular/forms';
import { HttpModule, JsonpModule }    from '@angular/http';

import { FileUploadModule } from 'ng2-file-upload';
import { D3Service } from 'd3-ng2-service';

*...*

@NgModule({
  imports: [ 
    BrowserModule, 
    FormsModule,
    HttpModule,
    JsonpModule,
    FileUploadModule,
    *...*
  ],
  declarations: [ 
    *...*
  ],
  providers: [  
    D3Service,
  ],
  bootstrap: [ 
    *...*
  ]
})

export class AppModule { }

我在控制台中遇到的错误是:

(function (global) {
  System.config({
    paths: {
      'npm:': 'node_modules/'
    },
    map: {
      'app': 'app',
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',

      'ng2-file-upload' : 'npm:ng2-file-upload',
      'd3-ng2-service' : 'npm:d3-ng2-service', 
    },
    packages: {
      app: {
        defaultExtension: 'js',
        meta: {
          './*.js': {
            loader: 'systemjs-angular-loader.js'
          }
        }
      },
      rxjs: {
        defaultExtension: 'js'
      },
      /** Configuration for ng2-file-upload */
      'ng2-file-upload' : { 
        main: './ng2-file-upload.js',
        defaultExtension: 'js'
      },
      'd3-ng2-service' : { 
        main: './index.js',
        defaultExtension: 'js'
      },
    }
  });
})(this);

我可以确认所有路径都是正确的。 在node_modules / d3-ng2-service / index.js中有一个语句:

GET http://localhost:3000/traceur 404 (Not Found)
Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:3000/traceur
    Error: XHR error (404 Not Found) loading http://localhost:3000/traceur
        at XMLHttpRequest.wrapFn [as __zone_symbol___onreadystatechange] (http://localhost:3000/node_modules/zone.js/dist/zone.js:1075:39)
        at ZoneDelegate.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:424:31)
        at Zone.runTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:191:47)
        at ZoneTask.invokeTask [as invoke] (http://localhost:3000/node_modules/zone.js/dist/zone.js:498:34)
        at invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:1370:14)
        at XMLHttpRequest.globalZoneAwareCallback (http://localhost:3000/node_modules/zone.js/dist/zone.js:1388:17)
    Error loading http://localhost:3000/traceur
    Unable to load transpiler to transpile http://localhost:3000/node_modules/d3-ng2-service/index.js
    Error loading http://localhost:3000/node_modules/d3-ng2-service/index.js as "d3-ng2-service" from http://localhost:3000/app/app.module.js
        at XMLHttpRequest.wrapFn [as __zone_symbol___onreadystatechange] (http://localhost:3000/node_modules/zone.js/dist/zone.js:1075:39)
        at ZoneDelegate.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:424:31)
        at Zone.runTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:191:47)
        at ZoneTask.invokeTask [as invoke] (http://localhost:3000/node_modules/zone.js/dist/zone.js:498:34)
        at invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:1370:14)
        at XMLHttpRequest.globalZoneAwareCallback (http://localhost:3000/node_modules/zone.js/dist/zone.js:1388:17)
    Error loading http://localhost:3000/traceur
    Unable to load transpiler to transpile http://localhost:3000/node_modules/d3-ng2-service/index.js
    Error loading http://localhost:3000/node_modules/d3-ng2-service/index.js as "d3-ng2-service" from http://localhost:3000/app/app.module.js

我是否需要一些额外的依赖项来阅读此声明? 我没有使用webpack,d3-ng2-service不能证明是.umd入口点。

2 个答案:

答案 0 :(得分:0)

我个人不使用d3-ng2-service。我目前正在使用Angular 9。

我这样导入它:

npm install d3 --save-dev

然后,在我的TypeScript文件中,将其导入为:

import * as d3 from 'd3';

然后,我像这样使用它:

private svg;
...
this.svg = d3.select('figure#chart')

我的HTML元素如下所示:

<div class="row">
  <div class="col-xs-12">
    <figure id="chart"></figure>
  </div>
</div>

我是Angular的初学者。因此,我不敢相信我必须是回答您问题的人。

答案 1 :(得分:0)

添加到 djangofan 的答案中,我还必须安装 d3 的 @types 以解决 TypeScript 错误:

npm install --save-dev @types/d3