在Angular 4中导入D3.js [英] Importing D3.js in Angular 4

查看:82
本文介绍了在Angular 4中导入D3.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从php迁移到Angular 2,没有太多的JavaScript或打字稿经验.因此,我需要您的帮助以填补空白.

I've just moved from php to Angular 2, without much javascript or typescript experience. Therefore I need your help filling in a gap.

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

I'm working on a rather large app, where I need to use D3.js. My development app is build on top of the official quickstart (https://github.com/angular/quickstart).

我正在尝试导入d3-ng2-service( https://www.npmjs.com/package/d3-ng2-service ),据我在SO和github上阅读的代码是正确的.但是,它不起作用.这是下面的一些代码,为简洁起见进行了编辑.

I'm trying to import d3-ng2-service (https://www.npmjs.com/package/d3-ng2-service) and as far as I read on SO and github the code is correct. However it doesn't work. Here's some code bellow, edited for brevity.

src/app/app.module.ts:

src/app/app.module.ts:

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 { }

src/systemjs.config.js

src/systemjs.config.js

(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);

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

The error I'm getting in console is this:

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

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

I can confirm that all paths are correct. In node_modules/d3-ng2-service/index.js there is a single statement:

export * from './src/d3.service';

我是否需要一些其他依赖项才能阅读此声明?我没有使用webpack,并且d3-ng2-service不能证明是.umd入口点.

Do I need some additional dependencies to read this statement? I am not using webpack and d3-ng2-service does not prove an .umd entry point.

推荐答案

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

Personally, I don't use d3-ng2-service. I am on Angular 9 at the moment.

我这样导入它:

npm install d3 --save-dev

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

Then, in my TypeScript file, I import it like:

import * as d3 from 'd3';

然后,我像这样使用它:

Then, I use it like so:

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

我的HTML元素如下所示:

And my HTML element looks like this:

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

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

I am a total beginner at Angular. So, I can't believe I have to be the person answering your question.

这篇关于在Angular 4中导入D3.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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