Cannot set property '' of null appears when axios all concurrent requests

Original code

created(){
    
    
    function getPetTypes(){
    
    
      return axios.get(url.getPetTypes)
    }
    function getProductTypes(){
    
    
      return axios.get(url.getProductTypes)
    }
    axios.all([getPetTypes(),getProductTypes()])
      .then(axios.spread(function(acct,perm){
    
    
        console.log(acct.data)
        this.petTypes = acct.data
        console.log(perm)
        
      }))
  },

An error occurs. The
Insert picture description here
data is still available here, but it cannot be assigned to an external variable.
Solution:
This is caused by the this point, so change the function to an arrow function

created(){
    
    
    function getPetTypes(){
    
    
      return axios.get(url.getPetTypes)
    }
    function getProductTypes(){
    
    
      return axios.get(url.getProductTypes)
    }
    axios.all([getPetTypes(),getProductTypes()])
      .then(axios.spread((acct)=>{
    
    
        console.log(acct.data)
        this.petTypes = acct.data
      }))
  },

Guess you like

Origin blog.csdn.net/d1063270962/article/details/114988526