Search code examples
javascriptvue.jsvuejs2nuxt.jsapexcharts

Nuxt.js: window is not defined apexcharts/vue-apexcharts


I have added this simple example from apexcharts.com. Pretty sure the imports are correct. I am not referencing window anywhere. My whole app just come to a halt when adding this file. I believe it has to do with the SSR or Nuxt configs.

Error message

<template>
  <div id="chart">
    <apexchart
      type="donut"
      width="380"
      :options="chartOptions"
      :series="series"
    ></apexchart>
  </div>
</template>

<script>
import VueApexCharts from "vue-apexcharts";

export default {
  name: "Donut",
  components: {
    apexchart: VueApexCharts,
  },
  data() {
    return {
      data: {
        series: [44, 55, 41, 17, 15],
        chartOptions: {
          chart: {
            type: "donut",
          },
          responsive: [
            {
              breakpoint: 480,
              options: {
                chart: {
                  width: 200,
                },
                legend: {
                  position: "bottom",
                },
              },
            },
          ],
        },
      },
    };
  },
};
</script>

Solution

  • As explained in my linked answer (last sentence, with the direct component syntax), here is how you make a proper working setup:

    <template>
      <div id="chart">
        <client-only>
          <apexchart
            type="donut"
            width="380"
            :options="chartOptions"
            :series="series"
          ></apexchart>
        </client-only>
      </div>
    </template>
    
    <script>
    export default {
      name: 'Donut',
      components: {
        [process.browser && 'apexchart']: () => import('vue-apexcharts'),
      },
      data() {
        return {
          series: [44, 55, 41, 17, 15],
          chartOptions: {
            chart: {
              type: 'donut',
            },
            responsive: [
              {
                breakpoint: 480,
                options: {
                  chart: {
                    width: 200,
                  },
                  legend: {
                    position: 'bottom',
                  },
                },
              },
            ],
          },
        }
      },
    }
    </script>
    

    I've also removed a data who was nesting the whole configuration, already inside of data(). This was causing a non-match in terms of props as shown in the browser console errors.