简体   繁体   English

在Angular2 / Node / Express / MongoDB App中使用d3.js

[英]Using d3.js in Angular2/Node/Express/MongoDB App

I'm trying to bring d3.js ( https://www.npmjs.com/package/d3 ) into my project and I get a ZoneAwareError on page load. 我正在尝试将d3.js( https://www.npmjs.com/package/d3 )引入我的项目,并且在页面加载时出现ZoneAwareError I have ran npm install d3 --save and everything got brought in just fine. 我已经运行npm install d3 --save ,一切都很好。 I followed the documentation on how to import it into my Angular2 Component but receive the below error on page load: 我遵循了有关如何将其导入到Angular2组件的文档,但是在页面加载时收到以下错误:

I should also note that all intellisense for d3 is showing up in Web Storm just fine. 我还应注意,d3的所有intellisense都可以在Web Storm中正常显示。

ZoneAwareError
  (anonymous function)  
  ZoneDelegate.invoke   
  Zone.run  
  (anonymous function)  
  ZoneDelegate.invokeTask   
  Zone.runTask  
  drainMicroTaskQueue   
  ZoneTask.invoke   

Component.ts Component.ts

import {Component, OnInit} from '@angular/core';
import * as d3 from 'd3';

@Component({
  selector: 'map-component',
  templateUrl: '/mlbstats.map.component.html',
  styleUrls: ['mlbstats.map.component.css']
})

export class MapComponent implements OnInit {
  constructor(){

  }
  ngOnInit(): void {
      d3.select('p');  
  }
}

If I comment out the line d3.select('p'); 如果我注释掉d3.select('p'); the page loads fine. 页面加载正常。 Is there an extra step that needs to be completed for this to work? 要完成此工作,是否需要完成一个额外的步骤? What exactly does the ZoneAwareError translate to? ZoneAwareError确切地转换为什么? I can't get a better error message so I'm not sure what is causing this. 我无法获得更好的错误消息,所以我不确定是什么原因引起的。 If I use the Chrome dev tools (opposed to the web storm dev tools) I get the below error: 如果使用Chrome开发工具(与Web Storm开发工具相对),则会出现以下错误:

(SystemJS) Unexpected token <↵ SyntaxError: Unexpected token <↵ at eval ()↵ at Object.eval ( http://localhost:3000/app/../public/pages/PortfolioComponent/MLBStats/MapComponent/mlbstats.map.component.js:15:10 )↵ at eval ( http://localhost:3000/app/../public/pages/PortfolioComponent/MLBStats/MapComponent/mlbstats.map.component.js:38:4 )↵ at eval ( http://localhost:3000/app/../public/pages/PortfolioComponent/MLBStats/MapComponent/mlbstats.map.component.js:39:3 )↵ at eval ()↵ Evaluating http://localhost:3000/d3.js ↵ Evaluating http://localhost:3000/app/../public/pages/PortfolioComponent/MLBStats/MapComponent/mlbstats.map.component.js ↵ Evaluating http://localhost:3000/app/../public/app.module.js ↵ Evaluating http://localhost:3000/app/../public/main.js ↵ Error loading http://localhost:3000/app/../public/main.js (SystemJS)意外令牌<↵语法错误:意外令牌<↵在eval()↵在Object.eval( http:// localhost:3000 / app /../ public / pages / PortfolioComponent / MLBStats / MapComponent / mlbstats.map。评估时为component.js:15:10 )(评估时为http:// localhost:3000 / app /../ public / pages / PortfolioComponent / MLBStats / MapComponent / mlbstats.map.component.js:38:4 ) ( http:// localhost:3000 / app /../ public / pages / PortfolioComponent / MLBStats / MapComponent / mlbstats.map.component.js:39:3 )↵在eval()↵评估http:// localhost:3000 /d3.js↵评估http:// localhost:3000 / app /../ public / pages / PortfolioComponent / MLBStats / MapComponent / mlbstats.map.component.js↵评估http:// localhost:3000 / app /。 /public/app.module.js↵评估http:// localhost:3000 / app /../ public / main.js↵加载http:// localhost:3000 / app /../ public / main.js时出错

Not the solution I was looking for but, removing import * as d3 from 'd3' and setting declare let d3: any above my @Component decorator solved the problem. 不是我一直在寻找的解决方案,而是import * as d3 from 'd3'删除import * as d3 from 'd3'并设置declare let d3: any @Component装饰器上方的declare let d3: any解决了问题。 I also had to add <script src="node_modules/d3/build/d3.js"></script> to my index.html file. 我还必须将<script src="node_modules/d3/build/d3.js"></script>到我的index.html文件中。

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

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