如何将d3.js导入角度2?

时间:2016-09-30 03:57:41

标签: javascript d3.js angular npm

我正在使用角度2的最终版本,没有角度cli和webpack。

import * as d3 from 'd3'; //ERROR: Cannot find module 'd3'

我安装了d3:  npm install d3 --save

问题是什么?

system.config.js:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',

      // angular bundles
      '@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',
      // other libraries
      'map':                        'npm:d3',
      'rxjs':                       'npm:rxjs',
      '@ngrx':                      'npm:@ngrx',
      'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      d3: {
        main: 'd3',
        format: 'global'
      },
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        main: '/bundles/Rx.min.js',
        defaultExtension: 'js'
      },
      '@ngrx/core': {
        main: 'bundles/core.min.umd.js',
        format: 'cjs'
      },
      '@ngrx/store': {
        main: 'bundles/store.min.umd.js',
        format: 'cjs'
      },
      '@ngrx/effects': {
        main: 'index.js',
        format: 'cjs'
      },
      '@ngrx/store-devtools': {
        main: 'bundles/store-devtools.min.umd.js',
        format: 'cjs'
      },
      '@ngrx/store-log-monitor': {
        main: 'bundles/store-log-monitor.min.umd.js',
        format: 'cjs'
      },
      '@ngrx/router-store': {
        main: 'bundles/router-store.min.umd.js',
        format: 'cjs'
      },
      'angular2-in-memory-web-api': {
        main: './index.js',
        defaultExtension: 'js'
      }
    }
  });
})(this);

typings.json:

{
  "globalDependencies": {
    "angular-protractor": "registry:dt/angular-protractor#1.5.0+20160425143459",
    "core-js": "registry:dt/core-js#0.0.0+20160725163759",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "node": "registry:dt/node#6.0.0+20160831021119",
    "selenium-webdriver": "registry:dt/selenium-webdriver#2.44.0+20160317120654"
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}

1 个答案:

答案 0 :(得分:3)

typings install --global --save dt~d3

您的代码中有一个JavaScript库(d3),并且您正尝试在TypeScript代码中导入它。 TypeScript不知道该库的“形状”,您需要安装TypeScript definition file才能使用它。它没有任何功能,只是库的“形状”,因此TypeScript编译器知道它正在使用它。