【问题标题】:How to import a package and call the function in angular?如何导入包并以角度调用函数?
【发布时间】:2020-08-26 14:44:10
【问题描述】:

我正在尝试导入一个d3-lasso 包以在angular (v10)d3 (v5) 上使用。

我使用npm 安装了d3-lasso

import { Component, OnInit, AfterViewInit, AfterContentInit, OnDestroy, ViewChild, ElementRef } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import * as d3 from 'd3';
import * as d3lasso from 'd3-lasso'; <-- this is how I'm importing the library

当在常规javascript 中使用它时,我们会将它用作d3.lasso(),它会正常工作。但是由于我使用的是角度,所以我收到以下错误。

Property 'lasso' does not exist on type 'typeof' import("..../@types/d3/index")'

所以,我尝试了几件事:

d3.d3lasso.lasso()
d3lasso.lasso()

但它们都排除了与上述类似的错误。如何调用这个lasso 函数以便在组件中使用它?

【问题讨论】:

  • 您要安装的是一个与 typescript 不兼容的 javascript 包,有人遇到像您这样的问题尝试检查它:github.com/skokenes/D3-Lasso-Plugin/issues/14
  • @RebaiAhmed,您提供的链接并没有说太多。该人说他们有类似的问题,然后说问题已解决,但不确定他是如何做到的。我想知道是否有什么办法可以让它发挥作用?
  • 你试过declare var d3lasso;而不是import然后用d3lasso.lasso()
  • @daddygames,你能详细说明一下吗?我不确定我是否关注
  • 您必须在 index.html 中使用 &lt;script&gt; 标记才能使声明工作。类似于您在 Angular 中使用 Jquery 的方式

标签: javascript angular d3.js


【解决方案1】:

我已经尝试过您的方案。我已经按照下面提到的步骤来执行 d3 js。

  1. 使用命令 npm install d3-lasso 安装 delasso 模块。
  2. 通过 import * as d3lasso from 'd3-lasso' 将 d3lasso 导入其中一个组件;
  3. 将 d3 脚本文件添加到 index.html&lt;script src="https://d3js.org/d3.v4.min.js"&gt;&lt;/script&gt;
  4. 然后在组件中声明它。 声明 var d3;
  5. 然后在示例的帮助下调用 d3 函数。

import * as d3lasso from 'd3-lasso';
declare var d3;

export class UsercomponentComponent implements OnInit {
 ngOnInit() {
   this.getLasso();
 }

 getLasso() {
   var data = new Array(100).fill(null).map(m=>[Math.random(),Math.random()]);
       var w = 960;
       var h = 500;
       var r = 3.5;
   var svg = d3.select("body").append("svg")
           .attr("width",w)
           .attr("height",h);
   var circles = svg.selectAll("circle")
           .data(data)
           .enter()
           .append("circle")
           .attr("cx",d=>d[0]*w)
           .attr("cy",d=>d[1]*h)
           .attr("r",r);
   var lasso = d3.lasso()
           .closePathSelect(true)
           .closePathDistance(100)
           .items(circles)
           .targetArea(svg)
           .on("start",this.lasso_start())
           .on("draw",this.lasso_draw())
           .on("end",this.lasso_end());
       
       svg.call(lasso);
 }
lasso_start() {
   d3lasso.items()
       .attr("r",3.5) // reset size
       .classed("not_possible",true)
       .classed("selected",false);
}

lasso_draw() {
       
 // Style the possible dots
 d3lasso.possibleItems()
     .classed("not_possible",false)
     .classed("possible",true);

 // Style the not possible dot
 d3lasso.notPossibleItems()
     .classed("not_possible",true)
     .classed("possible",false);
}

lasso_end() {
 // Reset the color of all dots
 d3lasso.items()
     .classed("not_possible",false)
     .classed("possible",false);

 // Style the selected dots
 d3lasso.selectedItems()
     .classed("selected",true)
     .attr("r",7);

 // Reset the style of the not selected dots
 d3lasso.notSelectedItems()
     .attr("r",3.5);

}
}

【讨论】:

  • 谢谢你。这很有帮助。但是,我收到d3.lasso is not a function 错误。我确实安装了d3-lasso,并且按照您的建议引用了d3。有什么想法吗?
  • 您能解释一下您的步骤吗? @Kuni
  • 此错误已修复且已正确加载。感谢@Muthupriya 帮助我解决了这个问题。我会接受这个作为答案。但是,我遇到了套索的另一个问题。如果你不介意可以看看这个here吗?
猜你喜欢
  • 2021-02-08
  • 2018-09-25
  • 2019-03-05
  • 1970-01-01
  • 2019-11-30
  • 2019-09-24
  • 2020-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多