[转]element中this.$message 失效问题解决方法(使用全局调用,重新定义this)(转载请删除括号里的内容)

 

这两天写项目的时候发现了这个问题。
问题再现:在Model框中操作数据,在使用this.$message进行消息提示时发现,提示框失效

本人解决方案(具体原因我没有找出来,写这个出来也是为了让大佬指点指点)

 // 保存修改数据
        handleSaveMu(row) {
            let srcColumnSqc = row.srcColumnSqc;
            let tarColumnName = row.tarColumnName;
            let tarColumn = row.tarColumn;
            let tarColumnType = row.tarColumnType;
            let id = row.id
            let apiType = this.muFormData.apiType;
            let srcTable = this.muFormData.srcTable;
            let jobName = this.muFormData.jobName;
            editMuTable(apiType,srcTable,srcColumnSqc,tarColumnName,tarColumn,tarColumnType,jobName,id).then(res=>{
                //console.log(res)
                if(res.data.code ==200){
                    this.$message.success(res.data.msg)  //数据请求成功,this.$message方法失效
                    this.checkMuVisible=false  
                    this.editDisabled2 = true
                }else{
                    this.$message.error(res.data.msg)
                    this.editDisabled2 = false
                }
            })
        },

解决方案一:

可以看到后台返回的数据使用this.$message就会失效,因为 this 指的不再是Vue了,所以… …

Vue.prototype.$message({
    message: '成功!',
    type: 'success'
});

使用全局变量Vue.prototype来调$message()就可以了。

解决方案二:

或者重新定义this也是可以的。如下:

   handleSaveMu(row) {
   			let _this = this;  //重新定义this
            let srcColumnSqc = row.srcColumnSqc;
            let tarColumnName = row.tarColumnName;
            let tarColumn = row.tarColumn;
            let tarColumnType = row.tarColumnType;
            let id = row.id
            let apiType = this.muFormData.apiType;
            let srcTable = this.muFormData.srcTable;
            let jobName = this.muFormData.jobName;
            editMuTable(apiType,srcTable,srcColumnSqc,tarColumnName,tarColumn,tarColumnType,jobName,id).then(res=>{
                //console.log(res)
                if(res.data.code ==200){
                    _this.$message.success(res.data.msg)
                    _this.checkMuVisible=false
                    _this.editDisabled2 = true
                }else{
                    _this.$message.error(res.data.msg)
                    _this.editDisabled2 = false
                }
            })
        },


---------------------
作者:ZHANGJIN9546
来源:CSDN
原文:https://blog.csdn.net/ZHANGJIN9546/article/details/103807180
版权声明:本文为作者原创文章,转载请附上博文链接!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值