Vue TypeError: this.$confirm is not a function

错误

在使用element ui,采用局部引入时候,报错TypeError: this.$confirm is not a function
在这里插入图片描述
因为没有在vue的实例上挂载 c o n f i r m 和 confirm和 confirmmessage导致的报错

解决方案

修改element.js文件:
1 引入messageBox 插件

import {MessageBox} from ‘element-ui’

2 在vue 的原型对象上挂载confirm

Vue.prototype.$confirm = MessageBox.confirm

如下图所示:
在这里插入图片描述 以后就可以放心的在vue中的任何文件使用this. c o n f i r m 或 者 t h i s . confirm或者this. confirmthis.message了。比如:你想用MessageBox中的confirm方法,现在可以这样用:

<template>
  <div>
    <el-button type="text" @click="dialogVisible = true">点击打开 Dialog</el-button>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    data () {
      return {
        dialogVisible: false
      }
    },
    methods: {
      handleClose (done) {
        const _this = this
        _this.$confirm('确认关闭?')
          .then(_ => {
            done()
          })
          .catch(_ => {
          })
      }
    }
  }
</script>
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁云亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值