Angular 2& ng2-charts:(SystemJS)意外指令' BaseChartComponent'进口

时间:2016-10-25 00:57:06

标签: angular chart.js systemjs ng2-charts

我正在尝试使用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 { }

当我运行应用程序时,我在浏览器控制台中收到的消息如下:

enter image description here

所以我在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,这是我得到的新错误:

enter image description here

这是app.component.ts的代码---请记住,在我尝试添加chart.jsng2-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());
    }
}

2 个答案:

答案 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'
      }