异步方法中无法识别vue代码

这是一段vue代码 调用了钉钉的免登陆前端代码 能取到值 但是无法把值存到自己定义的外部变量中

export default {
    name: "page2",
    data(){
      return{
        users:[],
        result: ['a', 'b'],
        num:[],
        FPKID:'',
        FCLR:''
      }
    },
    created(){
      dd.ready(function() {
        // dd.ready参数为回调函数,在环境准备就绪时触发,jsapi的调用需要保证在该回调函数触发后调用,否则无效。
        dd.runtime.permission.requestAuthCode({
          corpId: "ding2f6fc4afd61822c6f5",
          //这是一个异步函数
          onSuccess: function a(info) {
            //这句可以执行
            alert(info.code)
            //下面的都无法执行
            this.FCLR=info.code;
            this.$http.get('/mobile/'+info.code+'/'+"wj").then((res)=>{
              this.users=res.data;
              this.num=res.data;
            })
          },
        });
      });
    }
  }
vue
讨论数量: 1
 dd.ready(() => {
             console.log(this) // 罪魁祸首
            var _this = this;
        dd.runtime.permission.requestAuthCode(() =>{
          corpId: "ding2f6fc4afd61822c6f5",
          onSuccess: function a(info) {
            console.log(_this)
            _this.FCLR=info.code;
            _this.$http.get('/mobile/'+info.code+'/'+"wj").then(res =>{
              _this.users=res.data;
              _this.num=res.data;
            })
          },
        });
      });

闭包作用域问题。

function (){
  console.log(this)  // 指向window
}

() => {
  console.log(this)  // 指向父类的
}

详细百度

3年前 评论
ChrisMo (楼主) 3年前
lyxxxh (作者) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!