我有一个我无法解释的问题。
我使用接口实现创建了一个角度服务,但它告诉我,我有一个我无法解释的错误。
error TS2322: Type '{ labels: string[]; datasets: { data: number[]; backgroundColor: string[]; hoverBackgroundColor: string[]; }[]; }' is not assignable to type 'DataBase[]'.
对象文字只能指定已知属性,'DataBase []'类型中不存在'标签'。
我的代码:
Interface.ts
export interface Dashboardtwidget {
title: string;
widgetType: string;
datatype: DataBase[];
}
export interface DataBase {
labels: string[];
datasets: {
data: number[];
backgroundColor: string[];
hoverBackgroundColor: string[]
};
}
service.ts
import {Injectable} from '@angular/core';
import {Dashboardtwidget} from '../models/dashboard';
@Injectable()
export class DashboardService {
data: Dashboardtwidget[] = [
{
title: 'Widget 1',
widgetType: 'cardStyle1',
datatype: {
labels: ['A','B','C'],
datasets: [
{
data: [300, 50, 100],
backgroundColor: [
'#FF6384',
'#36A2EB',
'#FFCE56'
],
hoverBackgroundColor: [
'#FF6384',
'#36A2EB',
'#FFCE56'
]
}]
}
}
}
有没有人有过这个问题和这个错误信息?
因为我没有看到问题的来源
datasets
中的DataBase
在宣布为对象类型时看起来像Array
类型。
将您的DataBase
界面更改为:
export interface Dashboardtwidget {
title: string;
widgetType: string;
datatype: DataBase[];
}
interface Dataset {
data: number[];
backgroundColor: string[];
hoverBackgroundColor: string[];
}
export interface DataBase {
labels: string[];
datasets: Dataset[];
}
这是你的参考的Working Sample StackBlitz。