javascript - Why do I get asynchronous execution when I call the mutation method in vuex?
PHP中文网
PHP中文网 2017-05-18 10:50:21
0
2
508

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中文网

认证高级PHP讲师

reply all(2)
给我你的怀抱

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.

淡淡烟草味

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!