ChartJS Vue.js图表不工作?

lx0bsm1f  于 2022-11-06  发布在  Chart.js
关注(0)|答案(1)|浏览(226)

我正在尝试将chart.jsvue.js组合使用。
我有一个名为MessageGraph的组件,它看起来像这样(数据等来自文档):

<template>
    <canvas id="myChart" width="400" height="400"></canvas>
</template>

<script>
    import Chart from 'chart.js';

    export default {
        created() {
            var ctx = document.getElementById("myChart");
            var myChart = new Chart(ctx, {
                type: 'bar',
                data: {
                    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
                    datasets: [{
                        label: '# of Votes',
                        data: [12, 19, 3, 5, 2, 3],
                        backgroundColor: [
                            'rgba(255, 99, 132, 0.2)',
                            'rgba(54, 162, 235, 0.2)',
                            'rgba(255, 206, 86, 0.2)',
                            'rgba(75, 192, 192, 0.2)',
                            'rgba(153, 102, 255, 0.2)',
                            'rgba(255, 159, 64, 0.2)'
                        ],
                        borderColor: [
                            'rgba(255,99,132,1)',
                            'rgba(54, 162, 235, 1)',
                            'rgba(255, 206, 86, 1)',
                            'rgba(75, 192, 192, 1)',
                            'rgba(153, 102, 255, 1)',
                            'rgba(255, 159, 64, 1)'
                        ],
                        borderWidth: 1
                    }]
                },
                options: {
                    scales: {
                        yAxes: [{
                            ticks: {
                                beginAtZero:true
                            }
                        }]
                    }
                }
            });
        }
    }
</script>

但我在控制台中收到此错误:

app.c47f281….js:71222 [Vue warn]: Error in created hook: 
(found in <MessageGraph> at /Users/Janssen/Code/forumv2/resources/assets/js/components/graph/MessageGraph.vue)
warn @ app.c47f281….js:71222
handleError @ app.c47f281….js:72107
callHook @ app.c47f281….js:72939
Vue._init @ app.c47f281….js:74420
VueComponent @ app.c47f281….js:74584
createComponentInstanceForVnode @ app.c47f281….js:73779
init @ app.c47f281….js:73587
createComponent @ app.c47f281….js:75318
createElm @ app.c47f281….js:75261
createChildren @ app.c47f281….js:75386
createElm @ app.c47f281….js:75294
createChildren @ app.c47f281….js:75386
createElm @ app.c47f281….js:75294
patch @ app.c47f281….js:75753
Vue._update @ app.c47f281….js:72697
updateComponent @ app.c47f281….js:72820
get @ app.c47f281….js:73131
Watcher @ app.c47f281….js:73114
mountComponent @ app.c47f281….js:72824
./node_modules/vue/dist/vue.common.js.Vue$3.$mount @ app.c47f281….js:77914
./node_modules/vue/dist/vue.common.js.Vue$3.$mount @ app.c47f281….js:79963
Vue._init @ app.c47f281….js:74430
Vue$3 @ app.c47f281….js:74511
./resources/assets/js/app.js @ app.c47f281….js:80091
__webpack_require__ @ app.c47f281….js:20
0 @ app.c47f281….js:80854
__webpack_require__ @ app.c47f281….js:20
(anonymous) @ app.c47f281….js:66
(anonymous) @ app.c47f281….js:69
app.c47f281….js:72111 TypeError: Cannot read property 'length' of null
    at Object.acquireContext (app.c47f281….js:14468)
    at new Chart.Controller (app.c47f281….js:7776)
    at new Chart (app.c47f281….js:10193)
    at VueComponent.created (app.c47f281….js:2106)
    at callHook (app.c47f281….js:72937)
    at VueComponent.Vue._init (app.c47f281….js:74420)
    at new VueComponent (app.c47f281….js:74584)
    at createComponentInstanceForVnode (app.c47f281….js:73779)
    at init (app.c47f281….js:73587)

在我的app.js中,我添加了组件:

Vue.component('messageGraph', require('./components/graph/MessageGraph.vue'));

然后在我的appl.blade文件中,我有这样的内容:

<message-graph></message-graph>

有什么问题吗?

mepcadol

mepcadol1#

您正在访问created()挂钩中的canvas元素,该挂钩在模板附加到DOM之前被调用。
为了解决这个问题,你需要把代码放到组件的mounted()钩子中。但是,仍然不能保证模板在文档中。所以你必须使用$vm.nextTick()来确保DOM已经准备好。(源代码)。
此外,我建议使用vue的Child组件refs,它比document.getElementById()更像vue(它也能工作)。
第一个

相关问题