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