Chart.register不是一个函数(vue-chartjs官方示例不起作用)

huangapple go评论57阅读模式
英文:

Chart.register is not a function (vue-chartjs official example not working)

问题

我正在尝试将官方的'vue-charts.js'文档示例应用到我的Nuxt 2项目中:

<template>
  <Bar
    id="my-chart-id"
    :options="chartOptions"
    :data="chartData"
  />
</template>

<script>
import { Bar } from 'vue-chartjs'
import { Chart as ChartJS, Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale } from 'chart.js'

ChartJS.register(Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale)

export default {
  name: 'BarChart',
  components: { Bar },
  data() {
    return {
      chartData: {
        labels: ['January', 'February', 'March'],
        datasets: [{ data: [40, 20, 12] }]
      },
      chartOptions: {
        responsive: true
      }
    }
  }
}
</script>

但我收到了 chart_js__WEBPACK_IMPORTED_MODULE_1__.Chart.register is not a function 错误。我可能做错了什么?

附加信息:

"nuxt": "^2.15.7",
"vue-chartjs": "^5.2.0",
"chart.js": "^4.2.1"

英文:

I am trying to use the official 'vue-charts.js' documentation example into my nuxt 2 project:

&lt;template&gt;
  &lt;Bar
    id=&quot;my-chart-id&quot;
    :options=&quot;chartOptions&quot;
    :data=&quot;chartData&quot;
  /&gt;
&lt;/template&gt;

&lt;script&gt;
import { Bar } from &#39;vue-chartjs&#39;
import { Chart as ChartJS, Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale } from &#39;chart.js&#39;

ChartJS.register(Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale)

export default {
  name: &#39;BarChart&#39;,
  components: { Bar },
  data() {
    return {
      chartData: {
        labels: [ &#39;January&#39;, &#39;February&#39;, &#39;March&#39; ],
        datasets: [ { data: [40, 20, 12] } ]
      },
      chartOptions: {
        responsive: true
      }
    }
  }
}
&lt;/script&gt;

But i get chart_js__WEBPACK_IMPORTED_MODULE_1__.Chart.register is not a function error. What Can i be doing wrong?

Additional info:

"nuxt": "^2.15.7",
"vue-chartjs": "^5.2.0",
"chart.js": "^4.2.1"

答案1

得分: 1

尝试不要将 Chart 重命名为 ChartJS。
如果出现关于模块解析失败的错误,您需要使用 babel-loader 来转译 vue-chartjs 和 chartjs。
这主要是由于旧版本的 webpack 无法理解现代语法所致。
https://github.com/apertureless/vue-chartjs/issues/1002

英文:

Try to not rename Chart to ChartJS.
If you get an error regarding module parse failing, you need to use babel-loader to transpile vue-chartjs and chartjs

Mostly due to older webpack version that can't understand modern syntax.

https://github.com/apertureless/vue-chartjs/issues/1002

答案2

得分: 0

如果不将 Chart 重命名为 ChartJS 会发生什么?所以,可以这样写:

import { ChartJS, Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale } from 'chart.js';
ChartJS.register(Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale);
英文:

What happens if you don't rename Chart to ChartJS? So, instead, have

import { Chart, Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale } from &#39;chart.js&#39;;
Chart.register(Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale);

huangapple
  • 本文由 发表于 2023年3月7日 03:43:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定