在使用axios时,this已经不是本地的this了.

时间:2022-5-26    作者:老大夫    分类: JAVA


在使用axios时,this已经不是本地的this了,而是axios的this.
我们需要改变本地值时就需要先保存本地this在一个对象中,在axios中使用.

例如:以下代码将本地this暂存在that中,然后使用that进行本地数据操作.

methods:{
            getName:function (){
            //*************************
                var that=this;
            //*************************
                axios.get("http://localhost:8080/user/findall").then(
                    function (response){
                        console.log(response.data);
                        that.userName=response.data;
                    },
                    function (err){
                        console.log(err)
                    }
                )
            }
        }

标签: vue axios


扫描二维码,在手机上阅读

推荐阅读: