繁体   English   中英

错误TS2322:对象文字可能仅指定已知属性,并且类型中不存在“标签”

[英]error TS2322: Object literal may only specify known properties, and 'labels' does not exist in type

我有一个我无法解释的问题。

我使用接口实现创建了一个角度服务,但它告诉我我无法解释一个错误。

error TS2322: Type '{ labels: string[]; datasets: { data: number[]; backgroundColor: string[]; hoverBackgroundColor: string[]; }[]; }' is not assignable to type 'DataBase[]'.

对象文字只能指定已知的属性,而'label'在'DataBase []'类型中不存在。

这是我的代码:

接口

export interface Dashboardtwidget {
  title: string;
  widgetType: string;
  datatype: DataBase[];
}

export interface DataBase {
 labels: string[];
 datasets: {
 data: number[];
 backgroundColor: string[];
 hoverBackgroundColor: string[]
};
}

服务

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'
        ]
       }]
    }
  }
  }

有没有人遇到过此问题和此错误消息?

因为我看不出问题出在哪里

当您将其声明为对象类型时, DataBase datasets看起来就像一个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[];
}

这是供您参考的工作样本StackBlitz

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM