简体   繁体   中英

how to call function inside axios?

I am trying to call a function inside Axios but it showing undefined. Below is my simple code.i am using vuejs.

methods:{
    sett( ){
        console.log("result" )
    },

    List() {
        var self = this;
        axios.get("http://localhost:8080/api/v1/pas/device")
            .then(function(res) {
                self.sett( )
            }
        }
    },
    created: {
        this.List();
    }

Created is a function

methods:{
   sett(response){
       console.log(response)
       console.log("result" )
   },
},
created() {
   axios.get("http://localhost:8080/api/v1/pas/device").then(this.sett)
}

Use arrow function in axios and invoke other functions in regular way:

methods:{
  sett( ){
    console.log("result" )
  },
  List() {
    axios.get("http://localhost:8080/api/v1/pas/device")
        .then((res) => {
            this.sett();  // do with res what you want
        });
  }
},
created() {
    this.List();
}

created hook is a function not an object :

....
created:function(){
  this.List()
} 

or the ES6 shorthand syntax :

....
created(){
  this.List()
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM