8

版本:Cordova:6.3.1,Gulp CLI:1.2.2,Ionic 框架:2.0.0-rc.0,Ionic CLI 版本:2.1.0


我在我的 ionic2 应用程序中使用ng2-charts

导入不是import {ChartsModule} from "ng2-charts"; 因为import {ChartsModule} from "ng2-charts/components/charts/charts;"这个(issue #440)

我的整个app.module.ts

import { NgModule } from '@angular/core';
import { IonicApp, IonicModule } from 'ionic-angular';
import { ChartsModule } from 'ng2-charts/components/charts/charts';

import { MyApp } from './app.component';
import { EventsPage } from '../pages/events/events.component';
import { ChartComponent } from '../pages/chart/chart.component';
import { APICaller } from '../services/APICaller.service';
import { EventDetailComponent } from '../pages/event-detail/event-detail.component';
import { ParticipantDetail } from '../pages/participant-detail/participant-detail.component';
import { ParticipantFeedComponent } from '../pages/participant-feed/participant-feed.component';



@NgModule({
  declarations: [
    MyApp,
    EventsPage,
    EventDetailComponent,
    ParticipantDetail,
    ParticipantFeedComponent,
    ChartComponent
  ],
  imports: [
    IonicModule.forRoot(MyApp),
    ChartsModule
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    EventsPage,
    EventDetailComponent,
    ParticipantDetail,
    ParticipantFeedComponent,
    ChartComponent
  ],
  providers: [APICaller]
})
export class AppModule { }

图表组件

import { Component, OnInit } from '@angular/core';
@Component({
    selector: 'pie-chart',
    template: `

<base-chart
    class="chart"
    [datasets]="datasets"
    [labels]="labels"
    [options]="options"
    [chartType]="'doughnut'">Titel?
</base-chart>

`
})
export class ChartComponent implements OnInit {
    private datasets = [
        {
            label: "# of Votes",
            data: [12,19,3,5,2,3]
        }
    ];


    private labels = ['Red', 'blue', 'yellow', 'green', 'purple', 'orange'];

    private options = {
        scales: {
            yAxes: [{
                beginAtZero: true
            }]
        }
    };

    constructor() {

    }

    ngOnInit() { }
}

我认为这可能是Chart.js. 而且我发现在我的 aschart.js中引用它时,使用 npm 安装会导致 404 。index.htmlnode_modules/chart.js/src/chart.js

所以我下载了Chart.bundle.min.js在这不起作用之后我尝试下载Chart.js本身。(把它放在src/assets/js/...)。

ng2-charts\ng2-charts.js does not export ChartsModule 的评论部分也解决了这个问题。但是由于没有提供答案,而且这不是根本问题,所以我发布了这个。

错误:(抑制错误消息无济于事,因为它会阻止我的应用程序加载)。

polyfills.js:3 Unhandled Promise rejection: Template parse errors:
Can't bind to 'datasets' since it isn't a known property of 'base-chart'.
1. If 'base-chart' is an Angular component and it has 'datasets' input, then verify that it is part of this module.
2. If 'base-chart' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message.
 ("
    class="chart"

    [ERROR ->][datasets]="datasets"

    [labels]="labels"
"): ChartComponent@4:4
Can't bind to 'labels' since it isn't a known property of 'base-chart'.
1. If 'base-chart' is an Angular component and it has 'labels' input, then verify that it is part of this module.
2. If 'base-chart' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message.
 ("
    [datasets]="datasets"

    [ERROR ->][labels]="labels"

    [options]="options"
"): ChartComponent@6:4
4

1 回答 1

2

我从来没有使用过这个库,但是从最新(1.4.1)版本的源代码来看,它应该用作画布上的属性@Directive({selector: 'canvas[baseChart]'})

<canvas baseChart
   class="chart"
   [data]="datasets"
   [labels]="labels"
   [options]="options"
   [chartType]="'doughnut'">
</canvas>
于 2016-10-28T09:27:09.153 回答