Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJs, Vue-chartjs - mutating props

Tags:

So im getting this error

[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "chartData" (found in )

now this seems to be a common error but im not sure how to get around it in this case

im using Vue with Vue-chartjs and ive got the following

bar_chart.vue

<script>
  import { Bar, mixins } from 'vue-chartjs'

  export default Bar.extend({
    name:"bar_chart",
    mixins: [mixins.reactiveProp],
    props: ["width","height","options"],
    mounted () {

      this.renderChart(this.chartData, {responsive: true, maintainAspectRatio: false});
    }
  })
</script>

module_content.vue

<template>
  <div id="module_content">
    <div class="container-fluid">
      <div class="row">
        <div class="col-md-6 bar_chart_test">
          <bar_chart
            :chartData="DataSet"
            :width="400" 
            :height="200">
          </bar_chart>
          <button v-on:click="getBarDate">Hey there!</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import bar_chart from 'components/controls/bar_chart.vue'

export default {
  name: 'module_content',
  components: {
    bar_chart
  },
  data: function () {
    return { 
      data_source: {}
    }
  },
  methods: {
    getBarDate: function()
    {
        this.DataSet = ({
          labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
          datasets: [
            {
              label: 'GitHub Commits',
              backgroundColor: '#f87979',
              data: [40, 20, 12, 39, 10, 40, 39, 80, 40, 20, 12, 11]
            }
          ]
        });          
    }
  },
  computed: {
    DataSet: {
     cached: false,
      get: function () {
        return this.data_source
      },
      // setter
      set: function (newValue) {
        this.data_source = newValue;
      }
    }    
  }   
}
</script>

basically i want that function getBarDate to be able to set ChartData (replaced with an ajax call later down the line), been through hundreds of permutations and documents and still dont understand.

i think understand that you cant set it directly, thats fine, and the error is clear, but cant see any examples that work using the vue-chart mix in?

i have tried setting computed expressions or data variables in bar_chart.vue and same in module content.

i just cant seem to get it to behave

Can anyone help Thanks

like image 619
Saragan Avatar asked Mar 20 '17 16:03

Saragan


1 Answers

Don't mutate the prop directly. Use the prop to initialize a data value as in,

data: function () {
  return { 
    variableDataSet: this.Dataset,
  }
},

and emit an event when variableDataSet is changed,

watch: {
  variableDataSet: function(val){
    this.$emit('datasetchanged', val);
  }
}

which will be caught by the component using something like,

<bar_chart
  :chartData="DataSet"
  @datasetchanged="value => { DataSet = value }">
</bar_chart>
like image 73
Ishan Madhusanka Avatar answered Sep 22 '22 11:09

Ishan Madhusanka