封装了Elementui的提示框,报错了

这是饿了么官方给的例子:

this.$alert('这是一段内容', '标题名称', {
     confirmButtonText: '确定',
          callback: action => {
            this.$message({
              type: 'info',
              message: `action: ${ action }`
            });
          }
        });
      }

代码量太多,于是我想自己封装一个:

import { Message } from 'element-ui'

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

export default {alertOK}

设为全局,main.js:

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

调用时报错:

submit:function(){
    this.$common.alertOK("删除成功","提示");
}

clipboard.png

阅读 3.5k
2 个回答
export function alertOK(msg,title){
    this.$alert(msg, title, {
    ...
}

你的this指向不对呗,执行this.$common.alertOK时上面的this指向common

改成

    this.$common.alertOK.call(this,"删除成功","提示")
新手上路,请多包涵

this的指向问题。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题