Vue TypeError: this.$confirm is not a function

Dear 丶 2022-12-23 06:24 550阅读 0赞

错误

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

解决方案

修改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. confirm或者this.message了。比如:你想用MessageBox中的confirm方法,现在可以这样用:

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

发表评论

表情:
评论列表 (有 0 条评论,550人围观)

还没有评论,来说两句吧...

相关阅读