TypeError: Cannot set property 'options' of undefined

 

The data returned by axios call API is assigned to options, and an error is reported TypeError: Cannot set property 'options' of undefined
    axios.get('/api/ServerInfo/GetQueryTypedTSSST'
    ).then(function(res){

        this.options=res.data
       
    }).catch(function (error) {
        console.log(error);
    });

 

But already declared in the component

    data() {
      return {
        options:[],

 

In the  theninterior can not be instantiated using the Vue this, because the inside  this is not bound.

 

You can use ES6 arrow functions

     axios.get('/api/ServerInfo/GetQueryTypedTSSST'
    ).then((res)=>{
      this.options=res.data
    }).catch(function (error) {
        console.log(error);
    });

 

Or define that outside of axios

    var that=this
    axios.get('/api/ServerInfo/GetQueryTypedTSSST'
    ).then(function(res){

        this.options=res.data
       
    }).catch(function (error) {
        console.log(error);
    });

 

Guess you like

Origin www.cnblogs.com/JinweiChang/p/12719450.html