Vue uses axios to initiate a network request. After initiating the request, the vue component continues execution without waiting for the result to be returned.

questions such as the title, I believe this is the problem that will be encountered at the beginning of learning. Let me post my specific code

. In the

vue component, initiate a network request with the following code

clipboard.png

api

clipboard.png

chrome

clipboard.png

the result I want is to first output the "logout success" of logout in api and return success to the requestor, but it doesn"t happen. The reason is asynchronous, but I don"t know how to change it

.

in addition, if it is limited to the solution here, I also know that I may be asked to directly transfer the operation in the vue group (that is, the operation after receiving "success" to logout) to api. This can be solved here, but there will always be situations that are not suitable for this solution in the future, so I still hope that a boss can give you a solution that you think is appropriate in this situation

Jun.27,2022

you can use the async function

< hr >

api.js

logout(){
    return request(...);
}
< hr >

vue components

methods: {
    async xxxFun(){
        const result = await this.xxx.api.logout();
    }
}

return promise at logout, logout.then at components (. / / perform your later actions here), or use async,await.


the then method after a successful callback in the logout method should be placed in the Vue component

.
Menu