简体   繁体   English

在 Nuxt (3.0.0-rc.4) 中使用 vue-chartjs (4.1.1) 无法构建 | 如何在 nuxt 项目中使用简单的饼图?

[英]Using vue-chartjs (4.1.1) in Nuxt (3.0.0-rc.4) doesn't build | How can I use a simple pie chart in a nuxt project?

I want to use a reactive pie chart for my Nuxt app.我想为我的 Nuxt 应用程序使用反应式饼图。 For simplicity I thought chart.js is the most productive and easiest way to get fast results.为简单起见,我认为 chart.js 是获得快速结果的最有效和最简单的方法。 Now I am stuck for a couple of days.现在我被困了几天。 GitHub repos and other stackoverflow posts relate to older releases than mine. GitHub repos 和其他 stackoverflow 帖子与我的旧版本相关。

This is a minimal version of my app to reproduce the error I am trying to fix.这是我的应用程序的最小版本,用于重现我正在尝试修复的错误。

# installation
npx nuxi init repro-chartjs
cd repro-chartjs
yarn add vue-chartjs chart.js
yarn install

Open the project in an editor and add the pieChart.ts code from the official examples as a component into the project, eg components/pieChart.ts.在编辑器中打开项目并将官方示例中的 pieChart.ts 代码作为组件添加到项目中,例如 components/pieChart.ts。

import { defineComponent, h, PropType } from 'vue'

import { Pie } from 'vue-chartjs'
import {
  Chart as ChartJS,
  Title,
  Tooltip,
  Legend,
  ArcElement,
  CategoryScale,
  Plugin
} from 'chart.js'

ChartJS.register(Title, Tooltip, Legend, ArcElement, CategoryScale)

export default defineComponent({
  name: 'PieChart',
  components: {
    Pie
  },
  props: {
    chartId: {
      type: String,
      default: 'pie-chart'
    },
    width: {
      type: Number,
      default: 400
    },
    height: {
      type: Number,
      default: 400
    },
    cssClasses: {
      default: '',
      type: String
    },
    styles: {
      type: Object as PropType<Partial<CSSStyleDeclaration>>,
      default: () => {}
    },
    plugins: {
      type: Array as PropType<Plugin<'pie'>[]>,
      default: () => []
    }
  },
  setup(props) {
    const chartData = {
      labels: ['VueJs', 'EmberJs', 'ReactJs', 'AngularJs'],
      datasets: [
        {
          backgroundColor: ['#41B883', '#E46651', '#00D8FF', '#DD1B16'],
          data: [40, 20, 80, 10]
        }
      ]
    }

    const chartOptions = {
      responsive: true,
      maintainAspectRatio: false
    }

    return () =>
      h(Pie, {
        chartData,
        chartOptions,
        chartId: props.chartId,
        width: props.width,
        height: props.height,
        cssClasses: props.cssClasses,
        styles: props.styles,
        plugins: props.plugins
      })
  }
})

Use the PieChart component in app.vue.在 app.vue 中使用 PieChart 组件。

<template>
  <div>
    <PieChart />
  </div>
</template>

<script setup>
import PieChart from './components/pieChart'
</script>

Run the code.运行代码。

# dev build works
yarn dev -o

# production build doesn't work
yarn build
yarn preview

As commented, the development build runs perfectly fine.正如所评论的,开发版本运行得非常好。 But as soon as I try to test the production build, the terminal repeatedly throws me that error:但是,一旦我尝试测试生产版本,终端就会反复向我抛出该错误:

[nuxt] [request error] Named export 'ArcElement' not found. The requested module 'chart.js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'chart.js';
const { Chart, Title: Title$1, Tooltip, Legend, ArcElement, CategoryScale } = pkg;

  at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
  at async ModuleJob.run (node:internal/modules/esm/module_job:181:5)
  at async Promise.all (index 0)
  at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
  at async /C:/Users/panda/.00_Web-Dev/00%20Portfolio/repro-chartjs/.output/server/chunks/renderer.mjs:11158:24
  at async /C:/Users/panda/.00_Web-Dev/00%20Portfolio/repro-chartjs/.output/server/chunks/renderer.mjs:11213:64
  at async /C:/Users/panda/.00_Web-Dev/00%20Portfolio/repro-chartjs/.output/server/node_modules/h3/dist/index.mjs:420:19

  at async nodeHandler (/C:/Users/panda/.00_Web-Dev/00%20Portfolio/repro-chartjs/.output/server/node_modules/h3/dist/index.mjs:370:7)
  at async ufetch (/C:/Users/panda/.00_Web-Dev/00%20Portfolio/repro-chartjs/.output/server/node_modules/unenv/runtime/fetch/index.mjs:9:17)
  at async $fetchRaw2 (/C:/Users/panda/.00_Web-Dev/00%20Portfolio/repro-chartjs/.output/server/node_modules/ohmyfetch/dist/chunks/fetch.mjs:131:20)

Is it possible to have a working pie chart?是否有可能有一个有效的饼图? In my app the pie chart is fed with props and reacts nicely to user inputs.在我的应用程序中,饼图提供了道具,并且对用户输入做出了很好的反应。 I just can't deploy it for some reason, since the build doesn't work.由于某种原因,我无法部署它,因为构建不起作用。

My package.json:我的 package.json:

{
  "private": true,
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview"
  },
  "devDependencies": {
    "nuxt": "3.0.0-rc.4"
  },
  "dependencies": {
    "chart.js": "^3.8.0",
    "vue-chartjs": "^4.1.1"
  }
}

That's a known ESM issue.这是一个已知的 ESM 问题。 Background information and a solution are provided in the nuxt docs. nuxt 文档中提供了背景信息和解决方案。

Nuxt 3 Docs Nuxt 3 文档

Adding the problem causing library to build.transpile solves the issue.添加导致库到build.transpile的问题解决了该问题。

// nuxt.config.ts
import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
  build: {
    transpile: ['chart.js']
  }
})

Generates fine for me对我来说很好

Package.json Package.json

{
  "private": true,
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview"
  },
  "devDependencies": {
    "nuxt": "3.0.0-rc.4",
    "sass": "^1.53.0",
    "sass-loader": "^13.0.2",
    "vite-svg-loader": "^3.4.0",
    "webpack": "^5.73.0"
  },
  "dependencies": {
    "@pinia/nuxt": "^0.3.1",
    "chart.js": "^3.9.1",
    "chartjs-plugin-datalabels": "^2.1.0",
    "vue-chart-3": "^3.1.8",
    "vue-chartjs": "^4.1.1"
  }
}

My nuxt.config.js file我的 nuxt.config.js 文件

import { defineNuxtConfig } from 'nuxt'
import svgLoader from 'vite-svg-loader'

// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
    vite: {
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: `
                        @import "@/assets/styles.scss";
                        @import "@/assets/functions.scss";
                        @import "@/assets/variables.scss";
                        @import "@/assets/neumorphism.scss";
                        @import "@/assets/components.scss";
                    `,
                },
            },
        },
        plugins: [
          svgLoader({})
        ],
    },
    typescript: {
      shim: false
    },
    modules: [
      '@pinia/nuxt',
    ],
    build: {
      transpile: ['chart.js']
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM