vue項目中使用this.$confirm解析
vue使用this.$confirm
首先在element-ui中的el-table下的el-table-column中引入插槽(相當(dāng)于占位符)
?<template slot-scope="scope"> ? ? ? ? ? ? <el-button size="mini" @click="handleEdit(scope.$index, scope.row)" ? ? ? ? ? ? ? >編輯</el-button ? ? ? ? ? ? > ? ? ? ? ? ? <el-button ? ? ? ? ? ? ? size="mini" ? ? ? ? ? ? ? type="danger" ? ? ? ? ? ? ? @click="handleDelete(scope.$index, scope.row)" ? ? ? ? ? ? ? >刪除</el-button ? ? ? ? ? ? > ? ? ? ? ? </template>
?handleDelete(index, item) { ? ? ? this.$confirm("你確定要刪除嗎,請三思,后果自負", { ? ? ? ? confirmButtonText: "確定", ? ? ? ? cancelButtonText: "取消", ? ? ? ? type: "warning", ? ? ? }) ? ? ? ? .then(() => { ? ? ? ? ? console.log("確定了,要刪除"); ? ? ? ? }) ? ? ? ? .catch(() => { ? ? ? ? ? console.log("放棄了"); ? ? ? ? }); ? ? },
此時,需要在main.js中注冊組件
import {MessageBox} from 'element-ui'; //Vue.use(MessageBox);//與其他引用不同的是,這里“不能”加Vue.use(MessageBox),不然會出現(xiàn)問題,達不到想要的效果 Vue.prototype.$confirm = MessageBox.confirm;
vue TypeError: this.$confirm is not a function
錯誤
在使用element ui,采用局部引入時候,報錯TypeError: this.$confirm is not a function。
因為沒有在vue的實例上掛載confirm和message導(dǎo)致的報錯
解決方案
修改element.js文件:
1.引入messageBox 插件
import {MessageBox} from ‘element-ui'
2.在vue 的原型對象上掛載confirm
Vue.prototype.$confirm = MessageBox.confirm
如下圖所示:
以后就可以放心的在vue中的任何文件使用this.confirm或者this.confirm或者this.message了。比如:你想用MessageBox中的confirm方法,現(xiàn)在可以這樣用:
<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('確認(rèn)關(guān)閉?') .then(_ => { done() }) .catch(_ => { }) } } } </script>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
茶余飯后聊聊Vue3.0響應(yīng)式數(shù)據(jù)那些事兒
這篇文章主要介紹了茶余飯后聊聊Vue3.0響應(yīng)式數(shù)據(jù)那些事兒,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10實現(xiàn)Vue的markdown文檔可以在線運行的方法示例
這篇文章主要介紹了實現(xiàn)Vue的markdown文檔可以在線運行的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12Vue打包程序部署到Nginx 點擊跳轉(zhuǎn)404問題
這篇文章主要介紹了Vue打包程序部署到Nginx 點擊跳轉(zhuǎn)404問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02vue中將html字符串轉(zhuǎn)換成html后遇到的問題小結(jié)
這篇文章主要介紹了vue中將html字符串轉(zhuǎn)換成html后遇到的問題小結(jié),本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-12-12