uni-app 使用this动态传参赋值问题

    技术2022-07-11  85

    先上一段代码:

        export default {         data() {             return {                 token:''             }         }

    }

     let that=this

        uni.request({                     url:this.$BaseUrl+'/CompanyController/establishToken',                     methods:'POST',                     header:{                     'Content-type':'application/x-www-form-urlencoded'                     },                     dataType:"json",                     success:funtion(){                        // this.token=res.data.token;

                          that.token=res.data.token;                     }                 })

    这个代码中使用普通的success:funtion(){} 经常用普通函数 因此我们使用this.token赋值的时候出现怎么都赋不上值因为

    uni.request 中uccess方法指向闭包,所以this属于闭包,由此在success回调函数里是不能直接使用this.tokne的 可以在方法外使用let that=this的方法 that.token=res.data.token ;

    当然还有另外一种方法使用使用箭头函数直接使用this指向就可以

        uni.request({                     url:this.$BaseUrl+'/CompanyController/establishToken',                     methods:'POST',                     header:{                     'Content-type':'application/x-www-form-urlencoded'                     },                     dataType:"json",                     success:(res)=>{                         this.token=res.data.token;                     }                 })

      各位小姐姐,小哥哥 小编如果能够给各位解决问题,留下爱心,足迹哦

     

     

    Processed: 0.010, SQL: 9