0

There is such a code:

<template>

  <div class="chart"
       v-bind:style="chartStyleObject">

  </div>

</template>

<script>

export default{

data () {
  return {

    chartStyleObject: {
      width: this.$store.state.chartStyleObject.width,
      height: this.$store.state.chartStyleObject.height,
      marginTop: this.$store.state.chartStyleObject.marginTop,
      marginRight: this.$store.state.chartStyleObject.marginRight,
      marginBottom: this.$store.state.chartStyleObject.marginBottom,
      marginLeft: this.$store.state.chartStyleObject.marginLeft,
    },
  },
}
}

And such a repository:

const axios = require("axios");
export const state = () => ({
  chartStyleObject: {
    height: '247px',
    width: '500px',
    marginTop: '15px',
    marginRight: '0px',
    marginBottom: '0px',
    marginLeft: '15px',
  },
});


export const mutations = {
    changeChartDraggableEventState (state, EventState) {
        state.chartDraggableEventState = EventState;
    },
    changeChartHeight (state, height) {
        state.chartStyleObject.height = height;
    },
    changeHeightWrapper (state, HeightWrapper) {
        state.chartStyleObject.HeightWrapper = HeightWrapper;
    },
    changeWidthWrapper (state, WidthWrapper) {
        state.chartStyleObject.WidthWrapper = WidthWrapper;
    },
    changeChartMarginLeft (state, MarginLeft) {
        state.chartStyleObject.marginLeft = MarginLeft;
    },
    changeChartMarginTop (state, MarginTop) {
        state.chartStyleObject.marginTop = MarginTop;
    },
};

Problem:
If I change the state of the repository through mutations, then the properties of the repository change correctly. But! The data properties on which the same storage properties are tied for some reason does not change. (Despite the fact that repository property was changed)

My question is:
Why does this happen - if dates property, as well as repositories property, in theory, should be reactive? And which approach is the most correct in this case to solve this problem? (writing directly the storage properties in the code seems like a very cumbersome decision.)

1 Answer 1

2

When you assign the values in

chartStyleObject: {
  width: this.$store.state.chartStyleObject.width,          // here
  height: this.$store.state.chartStyleObject.height,
  marginTop: this.$store.state.chartStyleObject.marginTop,
  marginRight: this.$store.state.chartStyleObject.marginRight,
  marginBottom: this.$store.state.chartStyleObject.marginBottom,
  marginLeft: this.$store.state.chartStyleObject.marginLeft,
},

you are assigning values to the width, height and so on. You assign to them the current values of the state variables.

If you want the properties of chartStyleObject to change whenever the store's state changes, either map the state directly in the template (or wheverever you use it) or create a computed:

export default {
    computed: {
      chartStyleObject() {
        return {
          width: this.$store.state.chartStyleObject.width,
          height: this.$store.state.chartStyleObject.height,
          marginTop: this.$store.state.chartStyleObject.marginTop,
          marginRight: this.$store.state.chartStyleObject.marginRight,
          marginBottom: this.$store.state.chartStyleObject.marginBottom,
          marginLeft: this.$store.state.chartStyleObject.marginLeft,
        };
      },
    },
}
Sign up to request clarification or add additional context in comments.

3 Comments

Do I need usual data in this case and how should I change these computed properties from other methods in the future? If I just change my data properties against these computed properties - my app is not working.
Do I need watches in this case?
If you use the store, you have to change the store, you can't change the computed properties. The computed properties should be automatically updated once the state changes. You shouldn't need watches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.