简体   繁体   English

在Angular2中使用d3.js

[英]Using d3.js in Angular2

There are a few question on SO about it, but unfortunately they all seem to be deprecated. 关于SO,有几个问题,但是不幸的是,它们似乎都已被弃用。

Im using angular2 with angular-cli. 我在angular-cli中使用angular2。

To install d3.js im using npm install d3 . 要使用npm install d3 d3.js im, npm install d3

My app.component.ts file: 我的app.component.ts文件:

import { Component } from '@angular/core';
import * as d3 from 'd3';

@Component({
  selector: 'app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
}

But somehow, the app doesnt load correctly because of error: Cannot find module 'd3'. 但是由于某种原因,该应用程序由于错误而Cannot find module 'd3'.正确加载: Cannot find module 'd3'.

That's kinda strange, especially because the Webstorm is able to see the file and doesn't report any problems. 这有点奇怪,特别是因为Webstorm能够看到文件并且不报告任何问题。

I also tried to install the c3.js library and after installing ive tried the same import way: 我还尝试安装c3.js库,在安装ive之后尝试了相同的导入方式:

npm install c3

and

import * as c3 from 'c3';

But it doesnt work aswell as the first one. 但是它不像第一个那样工作。

EDIT! 编辑!

After using commands: 使用命令后:

npm install d3 --save

npm install @types/d3 --save-dev

like @Tudor Ciotlos mentioned, Im getting few errors. 就像@Tudor Ciotlos提到的那样,我收到的错误很少。

[default] C:\\Users\\node_modules\\@types\\c3\\index.d.ts:28:41 Generic type 'Selection' requires 4 type argument(s). [默认] C:\\ Users \\ node_modules \\ @types \\ c3 \\ index.d.ts:28:41通用类型“选择”需要4个类型参数。 [default] C:\\Users\\node_modules\\@types\\c3\\index.d.ts:351:56 [默认] C:\\ Users \\ node_modules \\ @types \\ c3 \\ index.d.ts:351:56

Module '" C:\\Users\\node_modules/@types/d3/index"' has no exported member 'Rgb'. 模块'“ C:\\ Users \\ node_modules / @ types / d3 / index”'没有导出的成员'Rgb'。 [default] C:\\Users\\node_modules\\@types\\c3\\index.d.ts:355:47 [默认] C:\\ Users \\ node_modules \\ @types \\ c3 \\ index.d.ts:355:47

Module '"C:/Users/node_modules/@types/d3/index"' has no exported member 'Rgb'. 模块'“ C:/ Users / node_modules / @ types / d3 / index”'没有导出的成员'Rgb'。 [default] C:\\Users\\ode_modules\\@types\\c3\\index.d.ts:833:51 [默认] C:\\ Users \\ ode_modules \\ @types \\ c3 \\ index.d.ts:833:51

Module '"C:/Users/node_modules/@types/d3/index"' has no exported member 'Rgb'. 模块'“ C:/ Users / node_modules / @ types / d3 / index”'没有导出的成员'Rgb'。 [default] C:\\Users\\node_modules\\@types\\c3\\index.d.ts:943:58 [默认] C:\\ Users \\ node_modules \\ @types \\ c3 \\ index.d.ts:943:58

Module '"C:/Users/node_modules/@types/d3/index"' has no exported member 'Rgb'. 模块'“ C:/ Users / node_modules / @ types / d3 / index”'没有导出的成员'Rgb'。

Anyone knows why Im getting these errors? 有人知道为什么我会收到这些错误吗?

In addition to installing the d3 package, you will also have to install the associated typings: 除了安装d3软件包外,您还必须安装关联的类型:

npm install d3 --save
npm install @types/d3 --save-dev

You can find more information about 3rd Party Library Installation and Global Library Installation in the angular-cli readme on GitHub. 您可以在GitHub上的angular-cli自述文件中找到有关第3方库安装全局库安装的更多信息。

This was happening to me as well - I am using angular-cli and d3 v4 and only getting errors in development. 这也发生在我身上-我正在使用angular-cli和d3 v4,并且在开发中只会出现错误。

In addition to import * as d3 from "d3"; 除了import * as d3 from "d3"; add the code below to your typings.d.ts file: 将以下代码添加到您的Types.d.ts文件中:

declare module 'd3' {
  export * from 'd3-array';
  export * from 'd3-axis';
  export * from 'd3-brush';
  export * from 'd3-chord';
  export * from 'd3-collection';
  export * from 'd3-color';
  export * from 'd3-dispatch';
  export * from 'd3-drag';
  export * from 'd3-dsv';
  export * from 'd3-ease';
  export * from 'd3-force';
  export * from 'd3-format';
  export * from 'd3-geo';
  export * from 'd3-hierarchy';
  export * from 'd3-interpolate';
  export * from 'd3-path';
  export * from 'd3-polygon';
  export * from 'd3-quadtree';
  export * from 'd3-queue';
  export * from 'd3-random';
  export * from 'd3-request';
  export * from 'd3-scale';
  export * from 'd3-selection';
  export * from 'd3-shape';
  export * from 'd3-time';
  export * from 'd3-time-format';
  export * from 'd3-timer';
  export * from 'd3-transition';
  export * from 'd3-voronoi';
  export * from 'd3-zoom';
}

Hope this helps! 希望这可以帮助!

Another path to explore is to use a ng library wrapping D3 functionality. 探索的另一条路径是使用包装D3功能的ng库。 For example, d3-ng2-service wraps D3 v.4 for consumption in Angular providing TS typings at the same time. 例如, d3-ng2-service包装D3 v.4以在Angular中消费,同时提供TS类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM