将highchartsjs添加到Vue3应用程序时出错

vatpfxk5  于 2022-11-10  发布在  Highcharts
关注(0)|答案(2)|浏览(453)

我正在使用Vue 3,并根据文档添加了highchartsjs。我得到了这个错误:

✘ [ERROR] Could not resolve "highcharts"

    node_modules/highcharts-vue/dist/highcharts-vue.min.js:1:90:
      1 │ ...?module.exports=e(require("highcharts"),require("vue")):"functio...
        ╵                              ~~~~~~~~~~~~

  You can mark the path "highcharts" as external to exclude it from the bundle,
  which will remove this error. You can also surround this "require" call with a
  try/catch block to handle this failure at run-time instead of bundle-time.

我尝试按照建议将其从包中排除,但没有效果:
vite.config.js

export default defineConfig({
  ...
  build: {
    rollupOptions: {
      external: ['highcharts'],
    }
  },
})
b4wnujal

b4wnujal1#

这是可行的:

export default defineConfig({
  ...
  optimizeDeps: {
    exclude: ['highcharts'],
  }
})
rkue9o1l

rkue9o1l2#

通过optimizeDeps.exclude排除highcharts可以清除错误,但这会使您在项目中使用highcharts的最终目标落空。您会注意到,在使用该配置后,您的项目仍然无法导入highcharts。该错误表明您的项目缺少该依赖项。
解决方案是安装highcharts

npm install -S highcharts

demo

相关问题