search

Home  >  Q&A  >  body text

javascript - Why do I get asynchronous execution when I call the mutation method in vuex?

I have a click function in the methods of a child component

takehash(index){
                   let hash = this.searchList[index].hash;
                   this.$store.commit('playvideo', hash);
                   console.log(this.$store.state.box.Url);
           }

There is a method in the mutations of store

playvideo(state,hash){
                   Vue.http.get("json.php"+hash).then(res=> {
                           let jsonObj = res.data;
                        state.box.Url= jsonObj.url;
                        console.log('no');
                   });
            }

But I found that the process I wanted was to commit the playvideo method, and after executing the playvideo method, the console came out with its url.
But the result was that the url was consoled first, and then the playvideo method was executed. Mutations are not a synchronous method. ?, why is the execution delayed? I am new to vuex, please ask someone to solve it

PHP中文网PHP中文网2779 days ago572

reply all(2)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-18 10:52:21

    Theoretically, mutations are only used to put synchronous methods. They cannot use asynchronous methods.
    And if you put an asynchronous method in your playvideo.
    Then the execution result must be asynchronous.

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-18 10:52:21

    Aren’t mutations a synchronous method?

    Synchronization of mutations means that you have to ensure that the methods in mutations are synchronous, not mutations that turn your asynchronous methods into synchronization.

    Asynchronous methods are placed in actions

    reply
    0
  • Cancelreply