D
D
danilr2019-03-15 20:08:07
Vue.js
danilr, 2019-03-15 20:08:07

Not fully reactive classes?

For some reason, reactive classes are not always reactive. There is a mutation in vuex, I go to this component, and for some reason the reactive classes are updated not immediately, but with some action in the component (that is, with updated)
How to update immediately when states change in vuex?
Here is a template with reactive classes

<div v-for="access in accessUnits" :key="access.id" class="action-item-box" @click="activeAccess(access.id)" :class="{'active-action': currentAccess === access.id}">
        <div class="action-item">
          <div class="item-value" 
            :class="{'checked-all': access.checkedAccess === 'all', 'checked-none': access.checkedAccess === 'none', 'checked-some': access.checkedAccess === 'some' }" 
            @click="toggleAccessUnits(access.id)"
          ></div>
          <div class="item-title">{{access.title}}</div>
        </div>
      </div>

getting data from vuex via computed
computed:{
    accessUnits(){
        return this.$store.state.administration.accessUnits   
    }
}

and here is the mutation that is called when you click on a new role (further, in order to display all the data that is considered here, you need to go to another component (with - you need it, you can’t remove it), where the reactive classes are not actually put down in time)
components
mutations: {
    getUserAccess(state, roleId) {

      for (let i = 0; i < state.accessUnits.length; i++) {  // сбрасываем галки на дефолтные - false
        for (let j = 0; j < state.accessUnits[i].data.length; j++) {
          state.accessUnits[i].data[j].is_available = false
        }
      }
      let userAccessId = []
      console.log('state.accessUnits: ', state.accessUnits);

      if (!state.user){
        let roleAccessUnits = state.roles.find(role => role.id === roleId).access_units
        userAccessId = roleAccessUnits.map(accessUnit => accessUnit.id)
      }
      else if (state.user && roleId){
        if (roleId === state.user.role_id){
          userAccessId = state.user.access_units.map(accessUnit => accessUnit.id)
        }
        else{
          let roleAccessUnits = state.roles.find(role => role.id === roleId).access_units
          userAccessId = roleAccessUnits.map(accessUnit => accessUnit.id)
        }
      }

      for (let i = 0; i < state.accessUnits.length; i++) {
        let countUnitsValue = 0
        for (let j = 0; j < state.accessUnits[i].data.length; j++) {
          if (userAccessId.includes(state.accessUnits[i].data[j].id)) {
            state.accessUnits[i].data[j].is_available = true
            countUnitsValue++
          }
        }
        if (countUnitsValue == 0){
          state.accessUnits[i].checkedAccess = 'none'
        }
        else if (countUnitsValue < state.accessUnits[i].data.length) {
          state.accessUnits[i].checkedAccess = 'some'
        }
        else if (countUnitsValue == state.accessUnits[i].data.length) {
          state.accessUnits[i].checkedAccess = 'all'
        }
      }
    },

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question