axios使用箭頭函數代替_this


方法里用this和方法里的方法里用this,指向的內容不一樣

不使用箭頭函數

    created(){
        let _this=this;
        axios.get('http://localhost:8081/findAll')
            .then(function(response) {
                _this.users = response.data;
                console.log(_this.users);
            }).catch(function (error) {
            console.log(error);
        })
    }

可以用箭頭函數代替_this。

箭頭函數錯誤用法

    created:()=>{
        axios.get('http://localhost:8081/findAll')
            .then( function(response) {
                this.users = response.data;
            }).catch(function (error) {
            console.log(error);
        })
    }

箭頭函數正確用法

    created(){
        axios.get('http://localhost:8081/findAll')
            .then( (response)=> {
                this.users = response.data;
            }).catch(function (error) {
            console.log(error);
        })
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM