ChartJS 语法错误:不能在Vue2图表js中的模块外部使用import语句

csbfibhn  于 2022-11-07  发布在  Chart.js
关注(0)|答案(1)|浏览(128)

使用版本)Vue 2:2.6.14、图表:2.9.4、图纸:4.1.0,验证/确认:1.12.3
组件\条形图.vue

<template>
  <Bar
    :chart-options="chartOptions"
    :chart-data="chartData"
    :chart-id="chartId"
    :dataset-id-key="datasetIdKey"
    :plugins="plugins"
    :css-classes="cssClasses"
    :styles="styles"
    :width="width"
    :height="height"
  />
</template>

<script type="module">
import { Chart as ChartJS, Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale } from 'chart.js'
ChartJS.register(Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale)
import { Bar } from 'vue-chartjs/legacy'

export default {
  name: 'BarChart',
  components: { Bar },
  props: {
    chartId: {
      type: String,
      default: 'bar-chart'
    },
    datasetIdKey: {
      type: String,
      default: 'label'
    },
    width: {
      type: Number,
      default: 400
    },
    height: {
      type: Number,
      default: 400
    },
    cssClasses: {
      default: '',
      type: String
    },
    styles: {
      type: Object,
      default: () => {}
    },
    plugins: {
      type: Object,
      default: () => {}
    },
    chartData: {
      labels: [ 'January', 'February', 'March' ],
      datasets: [ { data: [40, 20, 12] } ]
    },
    chartOptions: {
      responsive: true
    }
  },
  data() {
    return {

    }
  }
}
</script>

当尝试“npm run dev”时语法错误:无法在模块外部使用import语句已发生
当安装chart.js 3.7.1版本并更改代码时

import { Chart as ChartJS, Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale } from 'chart.js/auto'

有相同错误
我该怎么办?

7ajki6be

7ajki6be1#

最后我解决了它。删除下面的代码并引用此URL。Why does nuxt give me this error with vue-chartjs?

import { Chart as ChartJS, Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale } from 'chart.js' ChartJS.register(Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale)

相关问题