NG2-Charts 无法绑定到 'datasets',因为它不是 'canvas' 的已知 属性
NG2-Charts Can't bind to 'datasets' since it isn't a known property of 'canvas'
我正在尝试使用 NG2-Charts 的基本示例 (http://valor-software.com/ng2-charts/)
我复制粘贴了HTML部分
<div style="display: block">
<canvas baseChart
[datasets]="barChartData"
[labels]="barChartLabels"
[options]="barChartOptions"
[legend]="barChartLegend"
[chartType]="barChartType"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)"></canvas>
</div>
和 TypeScript 部分
private barChartOptions: any = {
scaleShowVerticalLines: false,
responsive: true
};
private barChartLabels: string[] = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
private barChartType: string = 'bar';
private barChartLegend: boolean = true;
private barChartData: any[] = [
{ data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' },
{ data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B' }
];
// events
private chartClicked(e: any): void {
console.log(e);
}
private chartHovered(e: any): void {
console.log(e);
}
我运行npm install ng2-charts --save
、npm install chart.js --save
和typings install dt~chart.js --save --global
我还在 vendor.ts 文件中导入了 chart.js
import 'chart.js';
我的依赖项:
{
"@angular/common": "^2.0.0",
"@angular/compiler": "2.0.0",
"@angular/core": "2.0.0",
"@angular/forms": "2.0.0",
"@angular/http": "2.0.0",
"@angular/platform-browser": "2.0.0",
"@angular/platform-browser-dynamic": "2.0.0",
"@angular/router": "3.0.0",
"angular2-jwt": "^0.1.18",
"chart.js": "^2.3.0",
"core-js": "^2.4.0",
"json-loader": "^0.5.4",
"moment": "^2.14.1",
"ng2-charts": "^1.4.0",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.11",
"zone.js": "^0.6.23"
}
这是错误
Unhandled Promise rejection: Template parse errors:
Can't bind to 'datasets' since it isn't a known property of 'canvas'. ("iuminfooneoptionsTwo">
<div style="display: block">
<canvas baseChart [ERROR ->][datasets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLeg"): PlayerprofileComponent@39:34
Can't bind to 'labels' since it isn't a known property of 'canvas'. (" <div style="display: block">
<canvas baseChart [datasets]="barChartData" [ERROR ->][labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLegend" [chartType]="barChart"): PlayerprofileComponent@39:60
Can't bind to 'options' since it isn't a known property of 'canvas'. ("lay: block">
<canvas baseChart [datasets]="barChartData" [labels]="barChartLabels" [ERROR ->][options]="barChartOptions" [legend]="barChartLegend" [chartType]="barChartType" (chartHover)="chartH"): PlayerprofileComponent@39:86
Can't bind to 'legend' since it isn't a known property of 'canvas'. (" <canvas baseChart [datasets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [ERROR ->][legend]="barChartLegend" [chartType]="barChartType" (chartHover)="chartHovered($event)" (chartClick)"): PlayerprofileComponent@39:114
Can't bind to 'chartType' since it isn't a known property of 'canvas'. ("ets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLegend" [ERROR ->][chartType]="barChartType" (chartHover)="chartHovered($event)" (chartClick)="chartClicked($event)">
"): PlayerprofileComponent@39:140 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse errors:
Can't bind to 'datasets' since it isn't a known property of 'canvas'. ("iuminfooneoptionsTwo">
<div style="display: block">
<canvas baseChart [ERROR ->][datasets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLeg"): PlayerprofileComponent@39:34
Can't bind to 'labels' since it isn't a known property of 'canvas'. (" <div style="display: block">
<canvas baseChart [datasets]="barChartData" [ERROR ->][labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLegend" [chartType]="barChart"): PlayerprofileComponent@39:60
Can't bind to 'options' since it isn't a known property of 'canvas'. ("lay: block">
<canvas baseChart [datasets]="barChartData" [labels]="barChartLabels" [ERROR ->][options]="barChartOptions" [legend]="barChartLegend" [chartType]="barChartType" (chartHover)="chartH"): PlayerprofileComponent@39:86
Can't bind to 'legend' since it isn't a known property of 'canvas'. (" <canvas baseChart [datasets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [ERROR ->][legend]="barChartLegend" [chartType]="barChartType" (chartHover)="chartHovered($event)" (chartClick)"): PlayerprofileComponent@39:114
Can't bind to 'chartType' since it isn't a known property of 'canvas'. ("ets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLegend" [ERROR ->][chartType]="barChartType" (chartHover)="chartHovered($event)" (chartClick)="chartClicked($event)">
"): PlayerprofileComponent@39:140
at TemplateParser.parse (eval at <anonymous> (http://localhost:8080/vendor.js:623:1), <anonymous>:133:19)
at RuntimeCompiler._compileTemplate (eval at <anonymous> (http://localhost:8080/vendor.js:2103:1), <anonymous>:244:51)
at eval (eval at <anonymous> (http://localhost:8080/vendor.js:2103:1), <anonymous>:167:83)
at Set.forEach (native)
at compile (eval at <anonymous> (http://localhost:8080/vendor.js:2103:1), <anonymous>:167:47)
at ZoneDelegate.invoke (eval at <anonymous> (http://localhost:8080/polyfills.js:4105:1), <anonymous>:192:28)
at Zone.run (eval at <anonymous> (http://localhost:8080/polyfills.js:4105:1), <anonymous>:85:43)
at eval (eval at <anonymous> (http://localhost:8080/polyfills.js:4105:1), <anonymous>:451:57)
at ZoneDelegate.invokeTask (eval at <anonymous> (http://localhost:8080/polyfills.js:4105:1), <anonymous>:225:37)
at Zone.runTask (eval at <anonymous> (http://localhost:8080/polyfills.js:4105:1), <anonymous>:125:47)
at drainMicroTaskQueue (eval at <anonymous> (http://localhost:8080/polyfills.js:4105:1), <anonymous>:357:35)
NG2-Charts 网站说我需要将其包含在 HTML
<script src="node_modules/chart.js/src/chart.js"></script>
但我认为那是不正确的,因为该声明如何在高效的缩小/构建的应用程序中工作?我还认为我的 webpack import 语句已经解决了这个问题?
我四处搜索,但遗憾的是只在 NG2 图表上找到了一些 Angular 2 Beta 版本的东西。
有没有人知道如何解决这个问题?
谢谢和干杯,
拉斐尔·希佩
尝试像这样在 app.module.ts 中导入 ChartsModule-
import { ChartsModule } from 'ng2-charts/ng2-charts';
imports: [
.....
ChartsModule
.....
]
在我的例子中,我不得不改变我的导入方式并在我的 html 文件标签中做一点改动:
chart.component.ts
import {Component} from '@angular/core';
import {CHART_DIRECTIVES} from 'ng2-charts/ng2-charts';
@Component({
selector: 'chart-balance',
template: require('./templates/chart-balance.template'),
styles: [`.chart {display: block; width: 100%;}`],
directives: [CHART_DIRECTIVES]
})
export class ChartBalanceComponent {
public barChartOptions:any = {
scaleShowVerticalLines: false,
responsive: true,
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: label => `${label.toLocaleString()}`
}
}]
}
};
public barChartLabels:string[] = ['2014', '2015', '2016'];
public barChartType:string = 'bar';
public barChartLegend:boolean = true;
public barChartData:any[] = [
{data: [32131, 3432, 543], label:'Test 1'},
{data: [54353, 432, 768], label:'Test 2'}
];
// events
public chartClicked(e:any):void {
console.log(e);
}
public chartHovered(e:any):void {
console.log(e);
}
}
这是我所做的另一个更改:
notice -- > base-chart class="chart" ...
图表-balance.template
<base-chart class="chart"
[datasets]="barChartData"
[labels]="barChartLabels"
[options]="barChartOptions"
[legend]="barChartLegend"
[chartType]="barChartType"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)"></base-chart>
ng2-图表
export * from './components/charts/charts';
declare var _default: {
directives: any[][];
};
export default _default;
图表
import { OnDestroy, OnInit, OnChanges, EventEmitter, ElementRef } from '@angular/core';
export declare class BaseChartComponent implements OnDestroy, OnChanges, OnInit {
static defaultColors: Array<number[]>;
data: number[] | Array<number[]>;
datasets: any[];
labels: Array<any>;
options: any;
chartType: string;
colors: Array<any>;
legend: boolean;
chartClick: EventEmitter<any>;
chartHover: EventEmitter<any>;
private ctx;
private cvs;
private parent;
private chart;
private initFlag;
private element;
constructor(element: ElementRef);
ngOnInit(): any;
ngOnChanges(): any;
ngOnDestroy(): any;
getChartBuilder(ctx: any): any;
private refresh();
}
export interface Color {
backgroundColor?: string | string[];
borderWidth?: number | number[];
borderColor?: string | string[];
borderCapStyle?: string;
borderDash?: number[];
borderDashOffset?: number;
borderJoinStyle?: string;
pointBorderColor?: string | string[];
pointBackgroundColor?: string | string[];
pointBorderWidth?: number | number[];
pointRadius?: number | number[];
pointHoverRadius?: number | number[];
pointHitRadius?: number | number[];
pointHoverBackgroundColor?: string | string[];
pointHoverBorderColor?: string | string[];
pointHoverBorderWidth?: number | number[];
pointStyle?: string | string[];
hoverBackgroundColor?: string | string[];
hoverBorderColor?: string | string[];
hoverBorderWidth?: number;
}
export interface Colors extends Color {
data?: number[];
label?: string;
}
export declare const CHART_DIRECTIVES: Array<any>;
我遇到了同样的问题。我在 github 上发现您也只需在子模块中插入 ChartsModule 即可。首先,您添加 app.module.ts
,在我的例子中,添加 reports.module.ts
.
我正在使用 ng2-charts + Angular 7 + Ionic 4,我花了几个小时寻找解决方案。这最终对我有用(当然,在执行初始步骤之后,比如安装 ng2-charts 和 charts.js)。只需在以下文件上导入 ChartsModule:
app.module.ts
import { ChartsModule } from 'ng2-charts';
...
imports: [ChartsModule]
yourPage.module.ts
import { ChartsModule } from 'ng2-charts';
@NgModule({
imports: [
IonicModule,
CommonModule,
FormsModule,
RouterModule.forChild([{ path: '', component: YourPagePage }]),
**ChartsModule**
]
})
我也试图在 yourPage.page.ts 中导入它,但解决方案是在 yourPage.module.ts 中导入它!
试试吧。
不是在应用程序模块中导入 ChartsModule,而是将其导入到您正在使用的内部模块中。例如,主页模块(如果在主页中使用图表)。
如果您使用的是共享模块,您还需要从那里导出 ChartsModule,以使其可供使用共享模块的所有组件使用。
// Simplified module only showing what's important for ChartsModule
import { NgModule } from '@angular/core';
import { ChartsModule } from 'ng2-charts';
@NgModule({
declarations: [],
imports: [ChartsModule],
exports: [ChartsModule]
})
export class SharedModule {
static forRoot(): ModuleWithProviders<SharedModule> {
return {
ngModule: SharedModule
};
}
}
如果您的组件是在 app.module.ts
以外的其他模块中声明的,您只需执行以下操作:
app.module.ts
import { ChartsModule } from 'ng2-charts';
...
@NgModule({
...
imports: [
...
ChartsModule,
...
]
})
export class AppModule{ }
module-where-your-component-is-declared.module.ts
import { ChartsModule } from 'ng2-charts';
...
@NgModule({
declarations: [YourComponent],
imports: [
...
ChartsModule,
...
],
...
})
export class ModuleWhereYourComponentIsDeclared { }
就我而言,这与在多个模块中包含 ChartsModule
或导出它无关。
我创建了一个使用 ng-chart
的图表组件,然后发现我需要它的另一个版本。所以我复制了组件,重命名了 class 和模板文件,但是忘记更新 @Component
装饰器中的 templateUrl
属性
两个不同的组件引用同一个模板文件给出了完全相同的错误:
Can't bind to 'datasets' since it isn't a known property of 'canvas'
我将新组件更改为使用正确的 HTML 模板后,错误就解决了。
仅供参考,我在我的图表组件模块 和引用它们的页面组件模块中引用了 ChartsModule
,如 @sanket 的 所示 (因此 AppModule
中不需要它)。
我正在与 ng2-chart@3.0.0-rc.2
合作
看起来 属性 chartType
现在称为 type
所以它应该看起来像这样:
<canvas
style="display: block;background-color: #3a3939;"
width="600"
height="300"
id="canvas"
#myCanvas
baseChart
[type]="lineChartType"
[datasets]="lineChartData"
[labels]="lineChartLabels"
[options]="mfChartOptions"
[legend]="lineChartLegend">
</canvas>
原因可能是: 您正在直接从 child components/module.
使用 Ng2Charts
解决方案: 您还必须在 parent 模块上导入 Ng2Charts。 (将 Ng2Charts [ng2-charts] 导入您的 parent 和 child 模块)
祝大家修复愉快。
谢谢。
Marjun Villegas
我正在尝试使用 NG2-Charts 的基本示例 (http://valor-software.com/ng2-charts/)
我复制粘贴了HTML部分
<div style="display: block">
<canvas baseChart
[datasets]="barChartData"
[labels]="barChartLabels"
[options]="barChartOptions"
[legend]="barChartLegend"
[chartType]="barChartType"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)"></canvas>
</div>
和 TypeScript 部分
private barChartOptions: any = {
scaleShowVerticalLines: false,
responsive: true
};
private barChartLabels: string[] = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
private barChartType: string = 'bar';
private barChartLegend: boolean = true;
private barChartData: any[] = [
{ data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' },
{ data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B' }
];
// events
private chartClicked(e: any): void {
console.log(e);
}
private chartHovered(e: any): void {
console.log(e);
}
我运行npm install ng2-charts --save
、npm install chart.js --save
和typings install dt~chart.js --save --global
我还在 vendor.ts 文件中导入了 chart.js
import 'chart.js';
我的依赖项:
{
"@angular/common": "^2.0.0",
"@angular/compiler": "2.0.0",
"@angular/core": "2.0.0",
"@angular/forms": "2.0.0",
"@angular/http": "2.0.0",
"@angular/platform-browser": "2.0.0",
"@angular/platform-browser-dynamic": "2.0.0",
"@angular/router": "3.0.0",
"angular2-jwt": "^0.1.18",
"chart.js": "^2.3.0",
"core-js": "^2.4.0",
"json-loader": "^0.5.4",
"moment": "^2.14.1",
"ng2-charts": "^1.4.0",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.11",
"zone.js": "^0.6.23"
}
这是错误
Unhandled Promise rejection: Template parse errors:
Can't bind to 'datasets' since it isn't a known property of 'canvas'. ("iuminfooneoptionsTwo">
<div style="display: block">
<canvas baseChart [ERROR ->][datasets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLeg"): PlayerprofileComponent@39:34
Can't bind to 'labels' since it isn't a known property of 'canvas'. (" <div style="display: block">
<canvas baseChart [datasets]="barChartData" [ERROR ->][labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLegend" [chartType]="barChart"): PlayerprofileComponent@39:60
Can't bind to 'options' since it isn't a known property of 'canvas'. ("lay: block">
<canvas baseChart [datasets]="barChartData" [labels]="barChartLabels" [ERROR ->][options]="barChartOptions" [legend]="barChartLegend" [chartType]="barChartType" (chartHover)="chartH"): PlayerprofileComponent@39:86
Can't bind to 'legend' since it isn't a known property of 'canvas'. (" <canvas baseChart [datasets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [ERROR ->][legend]="barChartLegend" [chartType]="barChartType" (chartHover)="chartHovered($event)" (chartClick)"): PlayerprofileComponent@39:114
Can't bind to 'chartType' since it isn't a known property of 'canvas'. ("ets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLegend" [ERROR ->][chartType]="barChartType" (chartHover)="chartHovered($event)" (chartClick)="chartClicked($event)">
"): PlayerprofileComponent@39:140 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse errors:
Can't bind to 'datasets' since it isn't a known property of 'canvas'. ("iuminfooneoptionsTwo">
<div style="display: block">
<canvas baseChart [ERROR ->][datasets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLeg"): PlayerprofileComponent@39:34
Can't bind to 'labels' since it isn't a known property of 'canvas'. (" <div style="display: block">
<canvas baseChart [datasets]="barChartData" [ERROR ->][labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLegend" [chartType]="barChart"): PlayerprofileComponent@39:60
Can't bind to 'options' since it isn't a known property of 'canvas'. ("lay: block">
<canvas baseChart [datasets]="barChartData" [labels]="barChartLabels" [ERROR ->][options]="barChartOptions" [legend]="barChartLegend" [chartType]="barChartType" (chartHover)="chartH"): PlayerprofileComponent@39:86
Can't bind to 'legend' since it isn't a known property of 'canvas'. (" <canvas baseChart [datasets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [ERROR ->][legend]="barChartLegend" [chartType]="barChartType" (chartHover)="chartHovered($event)" (chartClick)"): PlayerprofileComponent@39:114
Can't bind to 'chartType' since it isn't a known property of 'canvas'. ("ets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLegend" [ERROR ->][chartType]="barChartType" (chartHover)="chartHovered($event)" (chartClick)="chartClicked($event)">
"): PlayerprofileComponent@39:140
at TemplateParser.parse (eval at <anonymous> (http://localhost:8080/vendor.js:623:1), <anonymous>:133:19)
at RuntimeCompiler._compileTemplate (eval at <anonymous> (http://localhost:8080/vendor.js:2103:1), <anonymous>:244:51)
at eval (eval at <anonymous> (http://localhost:8080/vendor.js:2103:1), <anonymous>:167:83)
at Set.forEach (native)
at compile (eval at <anonymous> (http://localhost:8080/vendor.js:2103:1), <anonymous>:167:47)
at ZoneDelegate.invoke (eval at <anonymous> (http://localhost:8080/polyfills.js:4105:1), <anonymous>:192:28)
at Zone.run (eval at <anonymous> (http://localhost:8080/polyfills.js:4105:1), <anonymous>:85:43)
at eval (eval at <anonymous> (http://localhost:8080/polyfills.js:4105:1), <anonymous>:451:57)
at ZoneDelegate.invokeTask (eval at <anonymous> (http://localhost:8080/polyfills.js:4105:1), <anonymous>:225:37)
at Zone.runTask (eval at <anonymous> (http://localhost:8080/polyfills.js:4105:1), <anonymous>:125:47)
at drainMicroTaskQueue (eval at <anonymous> (http://localhost:8080/polyfills.js:4105:1), <anonymous>:357:35)
NG2-Charts 网站说我需要将其包含在 HTML
<script src="node_modules/chart.js/src/chart.js"></script>
但我认为那是不正确的,因为该声明如何在高效的缩小/构建的应用程序中工作?我还认为我的 webpack import 语句已经解决了这个问题? 我四处搜索,但遗憾的是只在 NG2 图表上找到了一些 Angular 2 Beta 版本的东西。
有没有人知道如何解决这个问题?
谢谢和干杯, 拉斐尔·希佩
尝试像这样在 app.module.ts 中导入 ChartsModule-
import { ChartsModule } from 'ng2-charts/ng2-charts';
imports: [
.....
ChartsModule
.....
]
在我的例子中,我不得不改变我的导入方式并在我的 html 文件标签中做一点改动:
chart.component.ts
import {Component} from '@angular/core';
import {CHART_DIRECTIVES} from 'ng2-charts/ng2-charts';
@Component({
selector: 'chart-balance',
template: require('./templates/chart-balance.template'),
styles: [`.chart {display: block; width: 100%;}`],
directives: [CHART_DIRECTIVES]
})
export class ChartBalanceComponent {
public barChartOptions:any = {
scaleShowVerticalLines: false,
responsive: true,
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: label => `${label.toLocaleString()}`
}
}]
}
};
public barChartLabels:string[] = ['2014', '2015', '2016'];
public barChartType:string = 'bar';
public barChartLegend:boolean = true;
public barChartData:any[] = [
{data: [32131, 3432, 543], label:'Test 1'},
{data: [54353, 432, 768], label:'Test 2'}
];
// events
public chartClicked(e:any):void {
console.log(e);
}
public chartHovered(e:any):void {
console.log(e);
}
}
这是我所做的另一个更改:
notice -- > base-chart class="chart" ...
图表-balance.template
<base-chart class="chart"
[datasets]="barChartData"
[labels]="barChartLabels"
[options]="barChartOptions"
[legend]="barChartLegend"
[chartType]="barChartType"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)"></base-chart>
ng2-图表
export * from './components/charts/charts';
declare var _default: {
directives: any[][];
};
export default _default;
图表
import { OnDestroy, OnInit, OnChanges, EventEmitter, ElementRef } from '@angular/core';
export declare class BaseChartComponent implements OnDestroy, OnChanges, OnInit {
static defaultColors: Array<number[]>;
data: number[] | Array<number[]>;
datasets: any[];
labels: Array<any>;
options: any;
chartType: string;
colors: Array<any>;
legend: boolean;
chartClick: EventEmitter<any>;
chartHover: EventEmitter<any>;
private ctx;
private cvs;
private parent;
private chart;
private initFlag;
private element;
constructor(element: ElementRef);
ngOnInit(): any;
ngOnChanges(): any;
ngOnDestroy(): any;
getChartBuilder(ctx: any): any;
private refresh();
}
export interface Color {
backgroundColor?: string | string[];
borderWidth?: number | number[];
borderColor?: string | string[];
borderCapStyle?: string;
borderDash?: number[];
borderDashOffset?: number;
borderJoinStyle?: string;
pointBorderColor?: string | string[];
pointBackgroundColor?: string | string[];
pointBorderWidth?: number | number[];
pointRadius?: number | number[];
pointHoverRadius?: number | number[];
pointHitRadius?: number | number[];
pointHoverBackgroundColor?: string | string[];
pointHoverBorderColor?: string | string[];
pointHoverBorderWidth?: number | number[];
pointStyle?: string | string[];
hoverBackgroundColor?: string | string[];
hoverBorderColor?: string | string[];
hoverBorderWidth?: number;
}
export interface Colors extends Color {
data?: number[];
label?: string;
}
export declare const CHART_DIRECTIVES: Array<any>;
我遇到了同样的问题。我在 github 上发现您也只需在子模块中插入 ChartsModule 即可。首先,您添加 app.module.ts
,在我的例子中,添加 reports.module.ts
.
我正在使用 ng2-charts + Angular 7 + Ionic 4,我花了几个小时寻找解决方案。这最终对我有用(当然,在执行初始步骤之后,比如安装 ng2-charts 和 charts.js)。只需在以下文件上导入 ChartsModule:
app.module.ts
import { ChartsModule } from 'ng2-charts';
...
imports: [ChartsModule]
yourPage.module.ts
import { ChartsModule } from 'ng2-charts';
@NgModule({
imports: [
IonicModule,
CommonModule,
FormsModule,
RouterModule.forChild([{ path: '', component: YourPagePage }]),
**ChartsModule**
]
})
我也试图在 yourPage.page.ts 中导入它,但解决方案是在 yourPage.module.ts 中导入它!
试试吧。
不是在应用程序模块中导入 ChartsModule,而是将其导入到您正在使用的内部模块中。例如,主页模块(如果在主页中使用图表)。
如果您使用的是共享模块,您还需要从那里导出 ChartsModule,以使其可供使用共享模块的所有组件使用。
// Simplified module only showing what's important for ChartsModule
import { NgModule } from '@angular/core';
import { ChartsModule } from 'ng2-charts';
@NgModule({
declarations: [],
imports: [ChartsModule],
exports: [ChartsModule]
})
export class SharedModule {
static forRoot(): ModuleWithProviders<SharedModule> {
return {
ngModule: SharedModule
};
}
}
如果您的组件是在 app.module.ts
以外的其他模块中声明的,您只需执行以下操作:
app.module.ts
import { ChartsModule } from 'ng2-charts';
...
@NgModule({
...
imports: [
...
ChartsModule,
...
]
})
export class AppModule{ }
module-where-your-component-is-declared.module.ts
import { ChartsModule } from 'ng2-charts';
...
@NgModule({
declarations: [YourComponent],
imports: [
...
ChartsModule,
...
],
...
})
export class ModuleWhereYourComponentIsDeclared { }
就我而言,这与在多个模块中包含 ChartsModule
或导出它无关。
我创建了一个使用 ng-chart
的图表组件,然后发现我需要它的另一个版本。所以我复制了组件,重命名了 class 和模板文件,但是忘记更新 @Component
装饰器中的 templateUrl
属性
两个不同的组件引用同一个模板文件给出了完全相同的错误:
Can't bind to 'datasets' since it isn't a known property of 'canvas'
我将新组件更改为使用正确的 HTML 模板后,错误就解决了。
仅供参考,我在我的图表组件模块 和引用它们的页面组件模块中引用了 (因此 ChartsModule
,如 @sanket 的 AppModule
中不需要它)。
我正在与 ng2-chart@3.0.0-rc.2
看起来 属性 chartType
现在称为 type
所以它应该看起来像这样:
<canvas
style="display: block;background-color: #3a3939;"
width="600"
height="300"
id="canvas"
#myCanvas
baseChart
[type]="lineChartType"
[datasets]="lineChartData"
[labels]="lineChartLabels"
[options]="mfChartOptions"
[legend]="lineChartLegend">
</canvas>
原因可能是: 您正在直接从 child components/module.
使用 Ng2Charts解决方案: 您还必须在 parent 模块上导入 Ng2Charts。 (将 Ng2Charts [ng2-charts] 导入您的 parent 和 child 模块)
祝大家修复愉快。
谢谢。 Marjun Villegas