簡體   English   中英

類型“IntrinsicAttributes”上不存在屬性“property”

[英]Property 'property' does not exist on type 'IntrinsicAttributes'

我是 React Typescript 的初學者。 我目前正在研究如何在 React Typescript App 中使用 Chart.js 實現條形圖。 所以我只想將屬性datasets:作為道具傳遞給 BarChart.tsx 組件。

所以這是我的 BarChart.tsx 組件:

import Chart from 'chart.js/auto';
import { Line } from 'react-chartjs-2';
import 'chartjs-adapter-luxon';
import StreamingPlugin from 'chartjs-plugin-streaming';

Chart.register(StreamingPlugin);

interface ChartProperty {
  label: string;
  backgroundColor: string;
  borderColor: string;
  borderDash?: number[];
  cubicInterpolationMode?: string;
  fill: boolean;
  data: [];
}

interface Props {
  property: ChartProperty[];
}

const BarChart: React.FC<Props> = ({property}) => {
  return (
    <div>
      <Line
        data={{
          datasets: property,
        }}
        height={100}
        width={400}
        options={{
          scales: {
            x: {
              type: 'realtime',
              realtime: {
                delay: 2000,
                onRefresh: (chart) => {
                  chart.data.datasets.forEach((dataset) => {
                    dataset.data.push({
                      x: Date.now(),
                      y: Math.random(),
                    });
                  });
                },
              },
            },
          },
        }}
      />
    </div>
  );
};

export default BarChart;

這是我的 App.tsx 組件:

import './App.css';
import BarChart from './components/BarChart';

const ChartProperty = [
  {
    label: 'Dataset 1',
    backgroundColor: 'rgba(255, 99, 132, 0.5)',
    borderColor: 'rgb(255, 99, 132)',
    borderDash: [8, 4],
    fill: false,
    data: [],
  },
  {
    label: 'Dataset 2',
    backgroundColor: 'rgba(54, 162, 235, 0.5)',
    borderColor: 'rgb(54, 162, 235)',
    cubicInterpolationMode: 'monotone',
    fill: false,
    data: [],
  },
];

const App = () => {
  return (
    <div>
      <h1 style={{ textAlign: 'center' }}>Sample Bar Chart</h1>
      <BarChart property={ChartProperty} />
    </div>
  );
};

export default App;

但是在這里,當我嘗試這個時,我在名為property的 prop 中的<BarChart property={ChartProperty} />中遇到錯誤。 所以這是我得到的錯誤:

TS2322: Type '{ property: ({ label: string; backgroundColor: string; borderColor: string; borderDash: number[]; fill: boolean; data: never[]; cubicInterpolationMode?: undefined; } | { label: string; backgroundColor: string; ... 4 more ...; borderDash?: undefined; })[]; }' is not assignable to type 'IntrinsicAttributes'.
  Property 'property' does not exist on type 'IntrinsicAttributes'.
    25 |     <div>
    26 |       <h1 style={{ textAlign: 'center' }}>Sample Bar Chart</h1>        
  > 27 |       <BarChart property={ChartProperty} />
       |                 ^^^^^^^^
    28 |     </div>
    29 |   );
    30 | };

在這種情況下有人可以幫助我嗎? 如果有人能顯示我的錯誤,我真的很感激。提前謝謝你。

你不能這樣寫: data: []; . 正確: data: any[]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM