Through the previous posts, we know how to set request headers and global parameters for Vue axios. This post will introduce: How to use Vue axios to execute synchronous network request?
Keywords: Vue , axios , synchronous , network , request.
.js file method
The method for Vue axios to execute network requests needs to be written in the .js file, and then the .js file need to be imported into the .vue file. For clarity, the .js file is named as NetWorkService.js.
.vue file method
We have already written NetWorkService.js to execute the network request. Now we need to import NetWorkService.js into the .vue file, instantiate NetWorkService, and then call the method in NetWorkService to execute the network request. It should be noted that in front of the function name that execute the network request, async needs to be used to turn an asynchronous request into a synchronous request.
complete .vue file
When using Vue axios to execute network requests, you need to write the request method in the NetWorkService.js file, then import and materialize NetWorkService.js in the .vue file, and finally use async to change the asynchronous request in NetWorkService.js into a synchronous request.