简体   繁体   中英

"TypeError: chart_js__WEBPACK_IMPORTED_MODULE_9__.default is not a constructor"

I'm attempting to use Chart.js v3.3.2 in a Vue component (by itself, since I need to use a plugin that requires v3), and I'm following the directions and a blog post , but I'm getting the following error:

Error in mounted hook: "TypeError: chart_js__WEBPACK_IMPORTED_MODULE_9__.default is not a constructor"

The relevant code (just trying to get it working initially) is this:

<template>
    <canvas id="myChart" />
<template>

<script>
import Chart from 'chart.js'

export default {
    mounted() {
            const ctx = document.getElementById('myChart');
            const stars = [135850, 52122, 148825, 16939, 9763];
            const frameworks = ['React', 'Angular', 'Vue', 'Hyperapp', 'Omi'];
            new Chart(ctx, {
                type: 'line',
                data: {
                    labels: frameworks,
                    datasets: [
                        {
                            label: "Github Stars",
                            data: stars,
                            backgroundColor: "rgba(255, 99, 132, 0.2)",
                            borderColor: "rgba(255, 99, 132, 1)",
                            borderWidth: 1
                        }
                    ]
                }
            })

    }

}

Chart is the default export from the library, so it doesn't look like I need to destructure in my import. Is this a bug, or am I doing something wrong?

According to the official docs in this section you could do:

And finally there is an separate path to do just the above for you, in one line:
import Chart from 'chart.js/auto'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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