vue中this在回调函数中的使用

this在各类回调中使用:

如果是普通函数是没法使用的

所以需要先将this.变量赋值给新的变量,然后才能在回调函数中使用

例如:
refund: function (id) {
      if (!this.url.refund) {
        this.$message.error("请设置url.refund属性!")
        return
      }
      var that = this;
      this.$confirm({
        title: "确认退款",
        content: "是否要进行退款?",
        onOk: function () {
          putAction(that.url.refund, { "id": id }).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.loadData();
            } else {
              that.$message.warning(res.message);
            }
          });
        }
      });
    },

如果是箭头函数式可以使用的

下面的这个例子只是参考,并不代表this.$confirm就是这么使用,具体参考ant的文档
下面的例子只是代表,箭头函数可以使用this
efund: function (id) {
      if (!this.url.refund) {
        this.$message.error("请设置url.refund属性!")
        return
      }
      this.$confirm({
        title: "确认退款",
        content: "是否要进行退款?",
        onOk: () => {
          putAction(that.url.refund, { "id": id }).then((res) => {
            if (res.success) {
              this.$message.success(res.message);
              this.loadData();
            } else {
              this.$message.warning(res.message);
            }
          });
        }
      });
    },
posted @   未月廿三  阅读(6538)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示