[英]How can I solve chart_js.Chart.register problem when using chart.js
正在使用 Next.js 開發用戶儀表板應用程序。 我從節點和 MongoDB 服務器查詢我的客戶端。 我需要使用 chart.js 加載我的圖表。 但是當我運行代碼時,我遇到了這個錯誤。 TypeError:chart_js.Chart.register 不是函數。 這是代碼片段:
import {Chart as ChartJS} from 'chart.js';
import { Bar } from 'react-chartjs-2';
import React, { useEffect, useReducer } from 'react';
import Layout from '../../components/Layout';
import { getError } from '../../utils/error';
ChartJS.register(
CategoryScale,
LinearScale,
BarElement,
Title,
Tooltip,
Legend
);
2 件事,你沒有導入你試圖注冊的所有元素,因為 Chart.register 不是一個函數,我懷疑你使用的是不可搖動的 Chart.js V2。 因此,除非您要升級到 V3 並使用 treeshaking,否則您不能也不必使用 register 來顯示圖表
如果您想在 V2 中注冊任何插件,您需要在圖表的插件上注冊它們,如下所示:
Chart.plugins.register(plugins);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.