F
F
fessss2021-10-13 20:06:16
Vue.js
fessss, 2021-10-13 20:06:16

Value not being recorded?

There is a component - a large form

data: {
  return {
    form: {
      // тут объект с глубокой вложенностью
    },
  }
}


Here is the method in actions where i make the request
async getDataFromApi({ commit }) {
    const res = await axios.get('url');
    if (res.data) {
      commit('mutationName', res.data);
    }
    return res;
  },


And here I call this action in the component
methods: {
  ...mapActions('api', ['getDataFromApi']),
  getData() {
    this.getDataFromApi().then(res => {
      if(res.status === 200 && res.data) {
        this.form = res.data; // тут this.form остается прежним, не меняется
      }
    })
  },
},


So, help me understand why this.form does not change.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Levchenko, 2021-10-13
@nuykon

Is this the whole code?
If so, then everything is simple, you do not call the method anywhere - getData () is the time.
Two - this method (getData() ) is not needed at all, since
...mapActions('api', ['getDataFromApi']), - has already added the getDataFromApi method, so
in the end you need to do it in the created/mounted hook or in another method call

methods: {
  ...mapActions('api', ['getDataFromApi']),
},
created() {
    this.getDataFromApi().then(res => {
      if(res.status === 200 && res.data) {
        this.form = res.data; // тут this.form должен изменится
      }
    })
},

well, it’s strange that you work with vuex in this case :)
apparently you should start with the basics of js, only then frameworks ...

M
Miki06, 2021-10-16
@Miki06

data must be a function that returns form
data(){
return{
form:{}
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question