简体   繁体   English

配置d3以与angular2和打字稿一起使用的正确方法

[英]proper way to configure d3 to work with angular2 and typescript

trying to include d3 library into an angular2 typescript project. 试图将d3库包含到angular2打字稿项目中。 I added d3 via npm install d3 and the typings via typing install d3 --save , the project local server doesn't start ( tsc && concurrently "npm run tsc:w" "npm run lite" ), with the following error: 我通过npm install d3添加了d3,通过typing install d3 --save添加了typing install d3 --save ,项目本地服务器无法启动( tsc && concurrently "npm run tsc:w" "npm run lite" ),出现以下错误:

typings/browser/definitions/d3/index.d.ts(3319,1): error TS2300: Duplicate identifier 'export='.
typings/browser/definitions/d3/index.d.ts(3323,1): error TS2300: Duplicate identifier 'export='.
typings/browser/definitions/d3/index.d.ts(3327,1): error TS2300: Duplicate identifier 'export='.
typings/modules/d3/index.d.ts(3319,1): error TS2300: Duplicate identifier 'export='.
typings/modules/d3/index.d.ts(3323,1): error TS2300: Duplicate identifier 'export='.
typings/modules/d3/index.d.ts(3327,1): error TS2300: Duplicate identifier 'export='.

these are my config files: 这些是我的配置文件:

typings.json: Types.json:

{
  "ambientDependencies": {
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
    "jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#5c182b9af717f73146399c2485f70f1e2ac0ff2b",
    "gapi": "github:DefinitelyTyped/DefinitelyTyped/gapi.auth2/gapi.auth2.d.ts"
  },
  "dependencies": {
    "d3": "registry:npm/d3#3.0.0+20160211003958"
  }
}

package.json: package.json:

{
  "name": "session-explorer",
  "version": "1.0.0",
  "scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.15",
    "systemjs": "0.19.26",
    "es6-shim": "^0.35.0",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.6.10",
    "d3": "^3.0.0"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "lite-server": "^2.2.0",
    "typescript": "^1.8.10",
    "typings": "^0.7.12"
  }
}

You can now install typings as follows: 您现在可以按照以下方式安装类型:

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

Then you can import d3 as follows 然后您可以按以下方式导入d3

import * as d3 from 'd3';

The 2017 Update 2017年更新

Installation 安装

Installing types for d3 v3 : d3 v3的安装类型:

npm install d3@3.x --save
npm install @types/d3@3.x --save-dev

Installing types for the latest d3 version (at the moment of writing v4 ): 最新d3版本的安装类型(在编写v4时 ):

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

Usage 用法

import * as d3 from 'd3';

As there is no typings available for D3 V4, we have to manually declare the d.ts for d3 some thing like 由于D3 V4没有可用的类型,因此我们必须手动为d3声明d.ts,例如

declare function d3(string: any): any;

After installing the D3 node module, we can import in file as 安装D3节点模块后,我们可以将文件导入为

var d3: any = require('d3');

From the error message it looks like you need to exclude your typings main.d.ts and main directories. 从错误消息中看来,您需要排除键入main.d.ts和main目录。

I would suggest adding a tsconfig.json in the same directory where your typings.json file is located. 我建议在您tingsings.json文件所在的目录中添加tsconfig.json。

tsconfig.json: tsconfig.json:

{
  "compilerOptions": {
      "target": "es5",
      "sourceMap": true,
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true,
      "module": "commonjs",
      "noImplicitAny": false
  },
  "exclude": [
      "node_modules",
      "typings/main.d.ts",
      "typings/main"
  ]
}

The angular documentation has a good introduction on how the tsconfig.json file works. 角度文档对tsconfig.json文件的工作方式进行了很好的介绍。

You should be able to import d3 directly with : 您应该可以直接使用导入d3:

import * as d3 from 'd3';

as long as the typings have been properly installed (which seems to be your case) and the actual d3.js file is loaded, either with a manual import or through some preprocessing task using node_modules/d3 folder. 只要正确安装了键入内容(这似乎是您的情况)并通过手动导入或通过使用node_modules / d3文件夹的某些预处理任务来加载实际的d3.js文件。

Also make sure that d3.js is not accidentally imported in 4.x version, as this version brings many changes that have not been integrated in the dt typings as of today. 还请确保d3.js不会在4.x版本中意外导入,因为此版本带来了许多更改,这些更改到目前为止尚未集成到dt类型中。

There are too many different answers here. 这里有太多不同的答案。 Because the maintain status of Typed d3. 因为类型为d3的维护状态。

For now, 2017/12/09, there is already d3 type, with latest version 4.12.0. 现在,2017/12/09,已经有d3类型,最新版本为4.12.0。 So no need to downgrade to version 3.x, or declare something. 因此,无需降级至3.x版或声明某些内容。

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

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