Encapsulated the prompt box of Elementui and reported an error

this is an example given by the official of ele.me:

this.$alert("", "", {
     confirmButtonText: "",
          callback: action => {
            this.$message({
              type: "info",
              message: `action: ${ action }`
            });
          }
        });
      }

there is too much code, so I want to package one myself:

import { Message } from "element-ui"

export function alertOK(msg,title){
    this.$alert(msg, title, {
        confirmButtonText: "",
        callback: action => {
            this.$message({
                type: "info"
            });
        }
    });
}

export default {alertOK}

set to global, main.js:

import common from "@/common/global.js"
Vue.prototype.$common = common

call Times error:

submit:function(){
    this.$common.alertOK("","");
}

clipboard.png


export function alertOK(msg,title){
    this.$alert(msg, title, {
    ...
}

your this points to the wrong one. When executing this.$common.alertOK , the above this points to common .

Change

to

    this.$common.alertOK.call(this,"","")

this pointing problem.

Menu