如何在axios内部调用函数?

时间:2020-11-09 07:58:19

标签: javascript vue.js axios

我正在尝试在Axios中调用一个函数,但显示未定义。下面是我的简单代码。我正在使用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();
    }

3 个答案:

答案 0 :(得分:1)

创建的是函数

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

答案 1 :(得分:1)

在axios中使用箭头功能并以常规方式调用其他功能:

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();
}

答案 2 :(得分:0)

created钩子是函数而不是对象:

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

或ES6速记语法:

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