简体   繁体   English

Vue.js:重新定义了图表数据

[英]Vue.js: Data for a chart is redefined

I use vue-chart.js to draw a simple chart. 我使用vue-chart.js绘制一个简单的图表。

import { Line } from 'vue-chartjs';

export default {
 extends: Line,
 mounted() {
   this.renderChart({
   labels: [this.getChartLabels],
   datasets: [
     {
       label: 'Active users',
       backgroundColor: '#f87979',
       data: [this.getChartData],
     },
   ],
 },
 { responsive: true,
   maintainAspectRatio: false,
 });
},
 computed: {
  ...mapGetters(['getFullReport', 'getChartLabels', 'getChartData']),
 },
 methods: {
  ...mapActions(['loadFullReport']),
 },
};

When I console my getter in mounted , I can see that I receive an array with two items but when I reload the page, the array appears to be empty. 当我将我的吸气剂mountedmounted ,可以看到我收到了一个包含两个项目的数组,但是当我重新加载页面时,该数组似乎为空。 The chart itself doesn't show any information neither before page reloading nor after. 图表本身在页面重新加载之前和之后都不会显示任何信息。 Please help! 请帮忙!

You should add a watcher which watches changes in your getters. 您应该添加一个观察器,以监视您的吸气剂中的变化。 And then update the chart over this.$data._chart.update() 然后通过this.$data._chart.update()更新图表

Chart.js is not reactive. Chart.js不具有反应性。 If your data changes or "comes to late" after your chart instance get rendered, you will see nothing. 如果在呈现图表实例后数据发生更改或“迟到”,则将看不到任何内容。

I guess you fill your store over an API ? 我猜您是通过API来填充商店吗? Then it is even more crutial, as your API calls are async. 然后,由于您的API调用是异步的,因此更为关键。

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

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