如何解决需要适当加载器的 npm 错误?

Err*_*ila 2 javascript npm vue.js chart.js vue-chartjs

我使用 vue-chartjs 创建了一个 Vue.js 项目。我尝试重新安装该库,但仍然出现此错误:

error  in ./node_modules/chart.js/dist/chart.esm.js
Module parse failed: Unexpected token (6613:12)
You may need an appropriate loader to handle this file type.
|         if (intermediateIndex1 !== startIndex && intermediateIndex1 !== lastIndex) {
|           decimated.push({
|             ...data[intermediateIndex1],
|             x: avgX,
|           });
 @ ./node_modules/vue-chartjs/es/BaseCharts.js 1:0-29
 @ ./node_modules/vue-chartjs/es/index.js
Run Code Online (Sandbox Code Playgroud)

应用程序.vue:

<template>
  <div id="app"></div>
</template>

<script>
import axios from "axios";
import moment from "moment";
import LineChart from "./components/LineChart";

export default {
  name: "App",
  components: {
    LineChart
  },
}
Run Code Online (Sandbox Code Playgroud)

折线图.vue

<script>
import { Line } from "vue-chartjs";

export default {
  extends: Line,
  props: {
    label: {
      type: String
    },
    chartData: {
      type: Array
    },
    options: {
      type: Object
    },
  },
  mounted() {
    const dates = this.chartData.map(d => d.date).reverse();
    const totals = this.chartData.map(d => d.total).reverse();

    this.renderChart(
      {
        labels: dates,
        datasets: [
          {
            label: this.label,
            data: totals
          }
        ]
      },
      this.options
    );
  }
};
</script>
Run Code Online (Sandbox Code Playgroud)

...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... …………

Lee*_*lee 5

您很有可能安装了 ChartJS 版本 3。vue 包装器与此版本的 Chart.js 不兼容,仅支持旧版本 2。

如果您通过将 package.json 中的版本号更改为 2.9.4 降级到版本 2.9.4,然后再次运行安装命令,或者删除包并使用命令install chart.js@2.9.4。这很可能会解决您的问题