簡體   English   中英

如何在 axios 內部調用函數?

[英]how to call function inside 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();
    }

Created 是一個函數

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

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

created鈎子是一個函數而不是一個對象:

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

或 ES6 速記語法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM