WJK
WJK

Reputation: 41

Syntax error : Cannot use import statement outside a module in Vue2 chart js

Using version) Vue2 : 2.6.14, chart.js : 2.9.4, vue-chartjs : 4.1.0, nuxtjs/vuetify: 1.12.3

components\BarChart.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>

When try "npm run dev" Syntax error : Cannot use import statement outside a module has occurred

When install chart.js 3.7.1 version and change code

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

has same error.

What should I do..?

Upvotes: 1

Views: 2588

Answers (1)

WJK
WJK

Reputation: 41

Finally I solve it. Delete below code and referece this 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)

Upvotes: 1

Related Questions