[英]error TS2322: Object literal may only specify known properties, and 'labels' does not exist in type
I have a problem that I can not explain. 我有一个我无法解释的问题。
I create an angular service with the interface implementation but it tells me that I have an error that I can not explain. 我使用接口实现创建了一个角度服务,但它告诉我我无法解释一个错误。
error TS2322: Type '{ labels: string[]; datasets: { data: number[]; backgroundColor: string[]; hoverBackgroundColor: string[]; }[]; }' is not assignable to type 'DataBase[]'.
Object literal may only specify known properties, and 'labels' does not exist in type 'DataBase[]'. 对象文字只能指定已知的属性,而'label'在'DataBase []'类型中不存在。
this my code : 这是我的代码:
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'
]
}]
}
}
}
Has anyone ever had this problem and this error message? 有没有人遇到过此问题和此错误消息?
Because I do not see where the problem comes from 因为我看不出问题出在哪里
datasets
in DataBase
looks like an Array
type when you've declared it as an Object type. 当您将其声明为对象类型时,
DataBase
datasets
看起来就像一个Array
类型。
Change your DataBase
interface to this: 将您的
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[];
}
Here's a Working Sample StackBlitz for your ref.
这是供您参考的工作样本StackBlitz 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.