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);
            }
          });
        }
      });
    },


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM