Angular 来自第三方声明模块的typescript导入功能
我已经运行了Angular 来自第三方声明模块的typescript导入功能,angular,typescript,d3.js,Angular,Typescript,D3.js,我已经运行了npm安装--save dev@types/d3-box 如何将其导入angular 7项目 我创建了一个文件custom-d3.ts,它看起来像: export * from 'd3-axis'; export * from 'd3-brush'; export * from 'd3-chord'; export * from 'd3-collection'; export * from 'd3-color'; export * from 'd3-contour'; export
npm安装--save dev@types/d3-box
如何将其导入angular 7项目
我创建了一个文件custom-d3.ts,它看起来像:
export * from 'd3-axis';
export * from 'd3-brush';
export * from 'd3-chord';
export * from 'd3-collection';
export * from 'd3-color';
export * from 'd3-contour';
export * from 'd3-dispatch';
export * from 'd3-drag';
export * from 'd3-dsv';
export * from 'd3-ease';
export * from 'd3-fetch';
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-random';
export * from 'd3-scale';
export * from 'd3-scale-chromatic';
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';
export * from 'd3-box';
在我的组件中,我将自定义d3作为d3导入,其中包括d3框。
import*作为d3从“../custom-d3”导入代码>
在我的代码中,我想调用d3.box(),如下所示:
this.boxPlot = d3.box()
.whiskers(this.iqr(1.5))
.height(this.graphHeight)
.showLabels(this.labels);
这就是我得到的错误:
ERROR in src/app/components/d3/group-graph/box-graph.ts(115,27): error TS2339: Property 'box' does not exist on type 'typeof import("custom-d3")'.
谢谢我相信为您的案例创建一个
首先,您需要创建一个新模块shared.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
//Import everything first
import * as d3Axis from 'd3-axis';
import * as d3Brush from 'd3-brush';
import * as d3Chord' from 'd3-chord';
// .. and so on
@NgModule({
declarations: [
//declare component here if you want to share it with all other components.
],
imports: [
CommonModule,
d3Axis,
d3Brush,
d3Chord
],
exports: [
d3Axis,
d3Brush,
d3Chord
]
})
export class SharedModule { }
然后,在您想要使用此模块的任何其他模块中,您只需在那里导入即可。多亏了Giacomo Debidda
然后当我使用它时:
this.boxPlot = (d3 as any).box()
既然已经安装了类型,为什么还需要一个custom-d3.ts
;从“d3盒”导入*作为d3盒;`但是无法让它工作,`this.boxPlot=d3Box.box().whiskers(this.iqr(1.5)).height(this.graphHeight)、showLabels(this.labels)`如果我尝试按您建议的方式执行此操作,则会出现此错误:'Type'typeof import(“/node_modules/@types/d3 axis/index”)'不可分配给Type'any[]| Type | ModuleWithProviders'。属性“ngModule”在类型“typeof import”(/node_modules/@types/d3 axis/index)中丢失,但在类型“ModuleWithProviders”中是必需的。ts(2322)core.d.ts(3755,5):“ngModule”在此处声明`你访问了我上面添加的链接吗?我刚刚向您展示了共享模块的概念,但您必须以适合您的应用程序的方式进行。
this.boxPlot = (d3 as any).box()