我正在尝试使用Node.js后端使用ng2-charts模块来处理我的Angular 2应用程序。这是我的app.module.ts
文件
import {NgModule} from "@angular/core";
import {BrowserModule} from "@angular/platform-browser";
import {FormsModule} from "@angular/forms";
import {HttpModule} from "@angular/http";
import AppComponent from "./app.component";
import TwitterService from "./twitter.service";
import BarGraphComponent from "./bar-graph.component";
import LineGraphComponent from "./line-graph.component";
import {CHART_DIRECTIVES } from "ng2-charts";
import {DataService} from "./data.service";
@NgModule({
imports: [BrowserModule, FormsModule, HttpModule ],
declarations: [AppComponent, BarGraphComponent, LineGraphComponent, CHART_DIRECTIVES],
bootstrap: [AppComponent],
providers: [TwitterService, DataService]
})
export default class AppModule { }
当我运行应用程序时,我在浏览器控制台中收到的消息如下:
所以我在node_modules/ng2-charts/ng2-charts.js
做了一些探索,这就是我所看到的:
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
var charts_1 = require('./components/charts/charts');
__export(require('./components/charts/charts'));
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = {
directives: [
charts_1.CHART_DIRECTIVES
]
};
然后我转到components/charts/charts.js
,这是代码的最后一行
exports.CHART_DIRECTIVES = [BaseChartComponent];
如何解决此问题?
POST-EDIT - 我更改了app.module.ts
,因此CHART_DIRECTIVES
现在位于declarations
,这是我得到的新错误:
这是app.component.ts
的代码---请记住,在我尝试添加chart.js
和ng2-charts.js
app.component.ts
import {Component, OnInit} from "@angular/core";
import {DataService} from "./data.service";
import TwitterService from "./twitter.service";
import {IoToken} from "./di";
import * as io from "socket.io-client";
@Component({
selector: "app",
templateUrl: "/app/app.component.html",
providers: [TwitterService, DataService, {provide: IoToken, useValue: io}]
})
export default class AppComponent implements OnInit {
topWords = this.data.topWords;
topWordsCount = this.data.topWordsCount;
topHashtags = this.data.topHashtags;
topHashtagsCount = this.data.topHashtagsCount;
tweets = this.data.tweets;
tweetsOverTime = this.data.tweetsOverTime;
getTweetCount = () => this.data.totalCount;
constructor(private twitter: TwitterService, private data: DataService) {}
ngOnInit(){
this.data.setSource(this.twitter.startStreaming());
}
}
答案 0 :(得分:0)
从上面提到的代码中,我理解的是您从Module中的ng2-charts库导入指令。
可能这是你遇到错误的原因。
尝试导入模块:
import { ChartsModule } from 'ng2-charts/ng2-charts';
// In your App's module:
imports: [
ChartsModule
]
希望这有帮助。
答案 1 :(得分:0)
我遇到了同样的问题。我将此添加到我的system.config.js
文件中以解决问题:
map: {
ng2-charts': 'npm:ng2-charts'
'ng2-charts': {
defaultExtension: 'js'
}