0

Всем привет,не получается вынести в геттеры небольшой функционал,подскажите как правильно это сделать Вот такая функция в компоненте

 methods: {
    fillDamages(num) {
      for(let ind in this.info.components) {
        if(this.info.components[ind].id == num) {
          this.info.components[ind].active = true;
          this.selected = this.info.components[ind];
          this.current = ind;
        } else {
          this.info.components[ind].active = false;
        }
      }
    },
    nextDamage() {
      let last = this.info.components.length - 1;

      this.info.components.map(item =>{
        if(item.id === this.selected.id && this.message){
          this.$set(item,'comment',this.message);
        }
      });
      this.message = '';

      if(this.current < last) {
        this.info.components[this.current].save = true;
        this.current++;
        for(let comp of this.info.components) {
            comp.active = false;
        }
        this.info.components[this.current].active = true;
        this.selected = this.info.components[this.current];
      } else {
        this.info.components[this.current].save = true;
        this.$router.push('/infocar')
      }
    },

И вот тут она используется

 <v-btn
            v-model="next"
            @click="nextDamage"
            class="btn-see"
            color="red">Далее</v-btn>

И тут

<v-card
          v-on:click.native="fillDamages(component.id)"
          v-for="component in info.components"
          :key="component.id"
          tabindex="1"
          class="item">
          <h3>{{component.serialNumber}} . {{component.name}}</h3>
          <v-icon v-if="component.comment" class="icon-damage" color="green">check_circle</v-icon>
        </v-card>

Вот как я пытаюсь сделать

import axios from 'axios'
export default {
  state:{
    info:[]
  },
  actions: {
    setDamages ({ commit }) {
      axios.get('api/v1/inspection/script', {
        headers: {
          Authorization: `Bearer ${localStorage.getItem('user-token')}`
        },
      }).then(response => {
        commit('setDamages', response.data)
      });
    },
    loadFile({commit},file){
      axios.post('api/v1/files/create', {count: 1},{headers:{Authorization: `Bearer ${localStorage.getItem('user-token')}`},})
        .then(() => axios.put('api/v1/files', file,{headers: {
          'content-type': 'multipart/form-data',
          Authorization:`Bearer ${localStorage.getItem('user-token')}`
        }}));
    },
  },
  mutations: {
    setDamages (state, payload) {
      state.info = payload
    }
  },
  getters: {
    nextDamages: (state) => (next) => {
      let last = this.info.components.length - 1;

      this.info.components.find(item =>{
        if(item.id === this.selected.id && this.message){
          this.$set(item,'comment',this.message);
        }
      });
      this.message = '';

      if(this.current < last) {
        this.info.components[this.current].save = true;
        this.current++;
        for(let comp of this.info.components) {
          comp.active = false;
        }
        this.info.components[this.current].active = true;
        this.selected = this.info.components[this.current];
      } else {
        this.info.components[this.current].save = true;
        this.$router.push('/infocar')
      }
    }
  }
}

в компоненте

nextDamage() {
      return this.$store.getters.nextDamages(this.next)
    },
            <v-btn
            v-model="next"
            @click="nextDamage"
            class="btn-see"
            color="red">Далее</v-btn>
16
  • А что в getter то нужно вынести? Или Вы хотите вынести methods в actions для store? 3 сен 2018 в 7:35
  • Все,что в methods
    – user294431
    3 сен 2018 в 7:41
  • Вынести это в геттеры стора,могу показать как я делаю,но я что-то не так делаю)
    – user294431
    3 сен 2018 в 7:42
  • Но гетеры это же то что возвращает данные, Вам по видимому вместо них нужны действия, которые вызвут мутации, вместо того чтобы менять данные внутри компонента они будут менять данные внутри state через мутации 3 сен 2018 в 7:46
  • Эмм,не знаю,мне ментор сказал в геттеры вынести(
    – user294431
    3 сен 2018 в 7:52

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.