错误 TS2322:对象文字只能指定已知属性,并且 'labels' 在类型中不存在
error TS2322: Object literal may only specify known properties, and 'labels' does not exist in type
我有一个问题无法解释。
我用接口实现创建了一个 angular 服务,但它告诉我有一个我无法解释的错误。
error TS2322: Type '{ labels: string[]; datasets: { data: number[]; backgroundColor: string[]; hoverBackgroundColor: string[]; }[]; }' is not assignable to type 'DataBase[]'.
对象字面量只能指定已知属性,'labels' 不存在于 '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[];
}
Here's a Working Sample StackBlitz for your ref.
我有一个问题无法解释。
我用接口实现创建了一个 angular 服务,但它告诉我有一个我无法解释的错误。
error TS2322: Type '{ labels: string[]; datasets: { data: number[]; backgroundColor: string[]; hoverBackgroundColor: string[]; }[]; }' is not assignable to type 'DataBase[]'.
对象字面量只能指定已知属性,'labels' 不存在于 '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[];
}
Here's a Working Sample StackBlitz for your ref.