我不知道drewmoore的suggestiong EIT她的。它似乎沒有爲我工作。
我有兩個建議繼續。
最佳答案
首先是增編的Arlo的答案。當我最初寫這篇評論時,我無法得到阿羅的答案。現在我明白了爲什麼。我正在嘗試使用d3層次結構包。所以安裝:
npm install d3 --save
npm install @types/d3 --save-dev
npm install @types/d3-hierarchy --save-dev
,然後使用該模塊是這樣的:
import * as d3 from "d3";
d3.hierarchy(blah);
然後它會抱怨它不知道的d3.hierarchy成員。現在我明白我必須使用這個對象(我不知道它爲什麼沒有註冊過)。因此,更新:
import * as d3 from "d3";
import * as d3Hierarchy from "d3-hierarchy";
d3Hierarchy.hierarchy(blah);
原來的答案
有什麼事,我發現迄今使用d3-ng2-service模塊位於鏈路的唯一答案。這不是一個好的開關,但它確實可以讓你在你的angular 2項目中使用d3v4。
自述的摘錄可以在下面找到如何在角2成分使用它:
import { Component, OnInit, ElementRef } from '@angular/core';
import { D3Service, D3, Selection } from 'd3-ng2-service'; // <-- import the D3 Service, the type alias for the d3 variable and the Selection interface
@Component({
selector: 'app-test-d3',
templateUrl: 'test-d3.component.html',
styleUrls: ['test-d33.component.css']
})
export class TestD3Component implements OnInit {
private d3: D3; // <-- Define the private member which will hold the d3 reference
private parentNativeElement: any;
constructor(element: ElementRef, d3Service: D3Service) { // <-- pass the D3 Service into the constructor
this.d3 = d3Service.getD3(); // <-- obtain the d3 object from the D3 Service
this.parentNativeElement = element.nativeElement;
}
ngOnInit() {
let d3 = this.d3; // <-- for convenience use a block scope variable
let d3ParentElement: Selection<any, any, any, any>; // <-- Use the Selection interface (very basic here for illustration only)
// ...
if (this.parentNativeElement !== null) {
d3ParentElement = d3.select(this.parentNativeElement); // <-- use the D3 select method
// Do more D3 things
}
}
}
我目前還沒有找到方法,所以我剛剛定義了類型爲Any的d3v4對象,並以我的快樂方式走了。當他們完成時,將帶來船上的類型。但如果有辦法,我也想知道。 –
你究竟做了什麼?我現在有'd3'作爲D3導入*;''但是'導入*作爲D3:任何'd3';'不起作用... – TommyF
我在它自己的腳本元素中加載d3到頁面,然後抓取它在我的控制器中使用擴展的angular.IWindowService,它只是增加了一個'd3:any'屬性。 –