简体   繁体   English

无法使用angular2像d3.svg(),d3.scale()和其他属性访问

[英]cant access like `d3.svg(), d3.scale()` and some others properties in d3js with angular2

I am trying to use d3.js in angular2 Iam using the below command to install d3 in Angular2 我正在尝试使用以下命令在angular2 Iam中使用d3.js在Angular2中安装d3

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

my package.json look like 我的package.json看起来像

{
  "name": "my-app",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.0.0",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "core-js": "^2.4.1",
    "d3": "^4.10.0",
    "rxjs": "^5.1.0",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.2.1",
    "@angular/compiler-cli": "^4.0.0",
    "@angular/language-service": "^4.0.0",
    "@types/d3": "^4.10.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.0.4",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3"
  }
}

and i add path to .angular-cli.json 我将路径添加到.angular-cli.json

some of the properties i cant access like d3.svg(), d3.scale() and some others 我无法访问的某些属性,例如d3.svg(), d3.scale()和其他一些属性

It returns error. 返回错误。

what could be the reason? 可能是什么原因?

You can import the specific method from d3, the same way rxjs operators. 您可以从d3导入特定方法,方法与rxjs运算符相同。

import { scaleLinear } from 'd3-scale';

You can use the method without the d3 namespace as: 您可以将没有d3名称空间的方法用于:

var x = scaleLinear();

Alternatively, you can import all 或者,您可以导入所有

import * as d3 from "d3";

var x = d3.scaleLinear();

Note that there is no scale in d3.v4 请注意,d3.v4中没有比例尺

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

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