vue項(xiàng)目中使用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("你確定要?jiǎng)h除嗎,請(qǐng)三思,后果自負(fù)", { ? ? ? ? confirmButtonText: "確定", ? ? ? ? cancelButtonText: "取消", ? ? ? ? type: "warning", ? ? ? }) ? ? ? ? .then(() => { ? ? ? ? ? console.log("確定了,要?jiǎng)h除"); ? ? ? ? }) ? ? ? ? .catch(() => { ? ? ? ? ? console.log("放棄了"); ? ? ? ? }); ? ? },
此時(shí),需要在main.js中注冊(cè)組件
import {MessageBox} from 'element-ui'; //Vue.use(MessageBox);//與其他引用不同的是,這里“不能”加Vue.use(MessageBox),不然會(huì)出現(xiàn)問題,達(dá)不到想要的效果 Vue.prototype.$confirm = MessageBox.confirm;
vue TypeError: this.$confirm is not a function
錯(cuò)誤
在使用element ui,采用局部引入時(shí)候,報(bào)錯(cuò)TypeError: this.$confirm is not a function。
因?yàn)闆]有在vue的實(shí)例上掛載confirm和message導(dǎo)致的報(bào)錯(cuò)
解決方案
修改element.js文件:
1.引入messageBox 插件
import {MessageBox} from ‘element-ui'
2.在vue 的原型對(duì)象上掛載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">點(diǎn)擊打開 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>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
茶余飯后聊聊Vue3.0響應(yīng)式數(shù)據(jù)那些事兒
這篇文章主要介紹了茶余飯后聊聊Vue3.0響應(yīng)式數(shù)據(jù)那些事兒,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10實(shí)現(xiàn)Vue的markdown文檔可以在線運(yùn)行的方法示例
這篇文章主要介紹了實(shí)現(xiàn)Vue的markdown文檔可以在線運(yùn)行的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12Vue打包程序部署到Nginx 點(diǎn)擊跳轉(zhuǎn)404問題
這篇文章主要介紹了Vue打包程序部署到Nginx 點(diǎn)擊跳轉(zhuǎn)404問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02vue3實(shí)現(xiàn)局部頁面刷新效果的示例詳解
這篇文章主要為大家詳細(xì)介紹了vue3如何采用 App.vue定義全局變量與方法并實(shí)現(xiàn)局部頁面刷新效果,文中的示例代碼講解詳細(xì),需要的可以參考一下2024-01-01vue中將html字符串轉(zhuǎn)換成html后遇到的問題小結(jié)
這篇文章主要介紹了vue中將html字符串轉(zhuǎn)換成html后遇到的問題小結(jié),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12基于Vue實(shí)現(xiàn)本地消息隊(duì)列MQ的示例詳解
這篇文章為大家詳細(xì)主要介紹了如何基于Vue實(shí)現(xiàn)本地消息隊(duì)列MQ, 讓消息延遲消費(fèi)或者做緩存,文中的示例代碼講解詳細(xì),需要的可以參考一下2024-02-02