You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue集成Chart.js添加数据时触发“too much recursion”递归错误的解决咨询

Vue集成Chart.js添加数据时触发“too much recursion”递归错误的解决咨询

我尝试把Chart.js作为Vue组件嵌入到我的应用里,可只要一尝试给图表的数据集添加新数据,就会抛出下面的递归错误:

Uncaught InternalError: too much recursion
    push reactivity.esm-bundler.js:798
    value helpers.collection.ts:138
    noTracking reactivity.esm-bundler.js:901
    push reactivity.esm-bundler.js:799
    value helpers.collection.ts:138
    noTracking reactivity.esm-bundler.js:901
    push reactivity.esm-bundler.js:799
    value helpers.collection.ts:138
    noTracking reactivity.esm-bundler.js:901
    push reactivity.esm-bundler.js:799
    value helpers.collection.ts:138
    noTracking reactivity.esm-bundler.js:901

Error Screenshot

我的代码结构如下:

App.vue

<script>
import Chart from "./Chart.vue";

export default {
    name: "Chart Demo",
    components: {
        Chart
    },
    data() {
        return {};
    }
};
</script>

<template>
    <div>
        <h1>Hello World</h1>
        <Chart></Chart>
    </div>
</template>

Chart.vue

<template>
    <canvas ref="canvas"></canvas>
</template>

<script>
import { defineComponent } from "vue";
import { Chart } from "chart.js/auto";

export default defineComponent({
    data() {
        return {
            chart: null,
            chartData: {
                labels: [
                    "Januar", 
                    "Februar", 
                    "März", 
                    "April", 
                    "Mai", 
                    "Juni", 
                    "Juli", 
                    "August"
                ],
                datasets: [{
                    label: "Verkäufe",
                    data: [60, 65, 67, 65, 66, 69, 72, 70]
                }]
            }
        };
    },
    mounted() {

        console.log("mounted create")

        this.chart = new Chart(this.$refs.canvas, {
            type: "line",
            data: this.chartData
        });

        setInterval(() => {

            console.log("Add chart data")

            let d = 90
            this.chartData.labels.push(d);
            this.chartData.datasets[0].data.push(d);

        }, 3000);

    }
});
</script>

如果去掉这段定时添加数据的代码,应用就能正常运行:

setInterval(() => {

    console.log("Add chart data")

    let d = 90
    this.chartData.labels.push(d);
    this.chartData.datasets[0].data.push(d);

}, 3000);

但我需要实现一个数据历史记录的功能,得不断给图表添加新数据并重新渲染。想请教一下,正确操作图表数据并重新渲染的方式应该是什么?

备注:内容来源于stack exchange,提问作者Marc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 16:43:01