例如如下代码(部分),利用回调函数实现登录成功后隐藏会话。

then(function(resp){
console.log(resp);
alert("登录成功");
that.dialogFormVisible = false;
}).catch(function(error){})
 
其中:dialogFormVisible是data中定义的数据

一般来说可以直接通过this.调用dialogFormVisible。

但是回调函数中无法识别this。导致 that.dialogFormVisible = false 该语句失效

解决方法一

在方法中定义变量储存this,例如var that = this; 然后再回调函数中使用that调用

解决方法二:

使用ES6语句

本例中更改代码为:

then( (resp) => {
console.log(resp);
alert("登录成功");
that.dialogFormVisible = false;
}).catch(function(error){})
————————————————
版权声明:本文为CSDN博主「Libra_Jayden」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44891863/article/details/113607496

相关文章:

  • 2021-05-24
  • 2021-11-01
  • 2022-01-23
  • 2021-09-17
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-26
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-12
相关资源
相似解决方案