使用 this.$modal.confirm().then() 方法做页面跳转时报错
源代码如下:
报错原因
this指向的对象取决与函数调用的上下文,在箭头函数中,this的值有最外层(最接近的非箭头函数)作用域决定。
解决方法
方法一:使用箭头函数或在‘then’方法中绑定‘this’
this.$modal.confirm('取消后本次编辑项不受保存,是否取消?').then(() => {
this.$emit("XX"); // 刷新index列表
this.$emit("close"); // 关闭当前页面
});
方法二:使用‘bind’方法显式绑定‘this'
this.$modal.confirm('取消后本次编辑项不受保存,是否取消?').then(function() {
this.$emit("XX"); // 刷新index列表
this.$emit("close"); // 关闭当前页面
}.bind(this));
方法三:将’this‘存储在一个变量中
var self = this;
this.$modal.confirm('取消后本次编辑项不受保存,是否取消?').then(function() {
self.$emit("XX"); // 刷新index列表
self.$emit("close"); // 关闭当前页面
});