Vue axios request data problem?

wrote a function to return data,

Vue.prototype.$http = axios

Vue.prototype.getData = function(url){
    let _data ="";
    this.$http.get(url).then(res=>{
        _data = res.data;
      });
    return _data;
}

if you can"t get the data in this way, the returned data is still empty. I don"t know how to solve it, or if there is any way to set up Synchronize on axios.

I want to get the data directly like this

this.list = this.getData (url);

When

or data, the data is returned directly

data:function () {

return this.getData(url);

}

Mar.06,2021

Local Synchronize writing can be achieved with async await .

Vue.prototype.getData = function(url){
   return this.$http.get(url).then(res=>res.data);
}
//
this.getData('url').then(res=>{
   console.log(res)
})
//async 
async function(){
  var data = await this.getData(url);
  console.log(data);
}

do not use Synchronize. You can define
as follows

Vue.prototype.getData = function(url){
    return axios.get(url).then(res=>{
        return res.data
    });
}

apply

this.getData(url).then(data => console.log(data)) 

you can refer to how nuxt.js 's asyncData is handled. I don't know whether you are server-side rendering or client-side rendering.
both server-side rendering and customer single rendering seem to have made some changes to the render method. Insert a piece of data.


Thank you for the invitation. This is a typical asynchronous scenario. Because the data on the server, the normal download takes several seconds, in order not to affect the normal use of users, so it will enter the asynchronous process, so you can not get it directly.

everything you say about the solution upstairs is basically right. The current general solution is to use Promise,. I just gave a lecture on Promise: n uses of Promise . I recommend you take a look.


async await to learn about typical async

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e40aa5-42e82.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e40aa5-42e82.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?