Vue的this.$confirm中注意this的指向

Vue开发过程中遇到this. c o n f i r m ( ) 里面的 t h i s 失效问题,就是当你想在里面使用 d a t a 数据的时候,我们往往是 t h i s . d a t a N a m e 这种方式拿到值,但在 t h i s . confirm()里面的this失效问题,就是当你想在里面使用data数据的时候,我们往往是this.dataName这种方式拿到值,但在this. confirm()里面的this失效问题,就是当你想在里面使用data数据的时候,我们往往是this.dataName这种方式拿到值,但在this.confirm()里面的this不是指向当前vue了,所以是取不到data的数据。

解决方法:
因此我们在使用this.$confirm()前先保存this

let _this = this
			const _this = this
            this.$confirm({
    
    
              title: '當前郵件正文内容爲空',
              content: h => <div style="color:red;">確認是否發佈?</div>,
              onOk () {
    
    
                console.log('保存提交的对象', this.objData)
                _this.loading = true
                initAxios.saveMail(_this.objData).then((res) => {
    
    
                  _this.loading = false
                  if (res.data.code === '200' && res.data.result) {
    
    
                    _this.$router.go(-1) // 处理返回需要点两次的问题
                    _this.$message.success('發佈成功!')
                  }
                })
              },
              onCancel () {
    
    
                return false
              }
            })

猜你喜欢

转载自blog.csdn.net/weixin_44786330/article/details/128897168
今日推荐