nuxt.js:窗口未定义apexcharts/vue-apexcharts

发布于 2025-01-21 16:29:12 字数 1323 浏览 1 评论 0原文

我添加了这个简单的示例,来自 apexcharts.com 。可以肯定的是,进口是正确的。我没有在任何地方引用窗口。添加此文件时,我的整个应用程序只是停顿。我相信这与SSR或NUXT配置有关。

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱娆 2025-01-28 16:29:12

我的链接答案(最后一句话,带有直接组件语法) :

<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>

我还删除了一个已经嵌套整个配置的data已经嵌套了data()的内部。如浏览器控制台错误所示,这导致了不匹配的道具。

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.

緦唸λ蓇 2025-01-28 16:29:12

将您的组件包装在NUXT的&lt;仅客户端&gt;组件中。试图引用不存在的窗口对象时,这将阻止SSR/SSG破坏。

例如

<client-only>
  <chart-component />
</client-only>

Wrap your component in nuxt's <client-only> component. That will prevent SSR/SSG breaking when attempting to reference the nonexistent window object.

E.g.

<client-only>
  <chart-component />
</client-only>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文