【问题标题】:I can't display the vue-chart.js on the page我无法在页面上显示 vue-chart.js
【发布时间】:2021-11-30 03:48:11
【问题描述】:

我正在尝试学习如何使用 vue-chart.js 和 Chart.js 。 问题是在 Main page.vue 上不显示图表本身

这是来自 ../src/Chart/RandomChart.vue 的代码

<template>
    <div>               
        <random-chart></random-chart>
    </div>
</template>

<script>
import RandomChart from '../Chart/RandomChart.vue'
export default{
    components:{
        RandomChart
    }

}
</script>

这是来自 ..src/Chart/LineChart.js 的代码

import { Line, mixins } from 'vue-chartjs'
const { reactiveProp } = mixins

export default {
  extends: Line,
  mixins: [reactiveProp],
  props: ['options'],
  mounted () {
    // this.chartData создаётся внутри миксина.
    // Если вы хотите передать опции, создайте локальный объект options
    this.renderChart(this.chartData, this.options)
  }
}

这是来自 RandomChart.vue 的代码,虽然它也在官方文档中

<template>
  <div class="small">
    <line-chart :chart-data="datacollection"></line-chart>
    <button @click="fillData()">Randomize</button>
  </div>
</template>

<script>
  import LineChart from './LineChart.js'

  export default {
    components: {
      LineChart
    },
    data () {
      return {
        datacollection: null
      }
    },
    mounted () {
      this.fillData()
    },
    methods: {
      fillData () {
        this.datacollection = {
          labels: [this.getRandomInt(), this.getRandomInt()],
          datasets: [
            {
              label: 'Data One',
              backgroundColor: '#f87979',
              data: [this.getRandomInt(), this.getRandomInt()]
            }, {
              label: 'Data One',
              backgroundColor: '#f87979',
              data: [this.getRandomInt(), this.getRandomInt()]
            }
          ]
        }
      },
      getRandomInt () {
        return Math.floor(Math.random() * (50 - 5 + 1)) + 5
      }
    }
  }
</script>

<style>
  .small {
    max-width: 600px;
    margin:  150px auto;
  }
</style>

嗯,来自 App.vue 的代码

<template>
    <div class="app">
        <router-view></router-view>
    </div>
</template>

<script>
    export default{
        
    }
</script>
<style scoped>
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
</style>

和 main.js

import { createApp } from 'vue'
import App from './App'
import router from "@/router/router";



const app = createApp(App)

app
    .use(router)
    .mount('#app');

和json

  "dependencies": {
    "chart.js": "^2.9.4",
    "core-js": "^3.6.5",
    "vue": "^3.0.0",
    "vue-chart-3": "^0.5.8",
    "vue-chartjs": "^3.5.1",
    "vue-router": "^4.0.11",
    "vuex": "^4.0.0-0"
  },

坐了半天不明白是什么问题,可能是我没看到的愚蠢错误。

【问题讨论】:

    标签: javascript vue.js chart.js diagram vue-chartjs


    【解决方案1】:

    vue-chartjs 与 chart.js v3(最新版本)不兼容,因此您需要将 chart.js 降级到 2.9.4 版才能使其工作,或者您需要使用不带 vue 包装器的 chart.js 准系统:

    npm uninstall chart.js
    npm install chart.js@2.9.4
    

    【讨论】:

    • 像在vue.js中我可以使用图表,没有vue图表的js吗?我一直在尝试将图表添加到 Main 2 天,但是零
    • 我不明白你对 vue 图表的意思,但如果你想为 chart.js 使用 vue 包装器,你需要将 chart.js 降级到 v2,否则你可以放弃 vue 包装器而只是像在默认 js 脚本中一样使用 chart.js 准系统,然后可以使用 v3
    • 那么,我该如何在我的应用中使用 (char.js) 呢?例如,这里是我要使用的图表apexcharts.com/vue-chart-demos/pie-charts/simple-pie-chart 如何将其作为页面元素加载到 Main 中。请告诉我
    • `
    猜你喜欢
    • 1970-01-01
    • 2020-01-03
    • 2020-10-20
    • 1970-01-01
    • 2020-11-12
    • 2015-05-06
    • 2021-07-14
    • 1970-01-01
    • 2021-05-07
    相关资源
    最近更新 更多