vue實(shí)現(xiàn)原理this.$message實(shí)例詳解
vue實(shí)現(xiàn)原理this.$message
components 通用組件文件夾下 創(chuàng)建 esConfirm文件夾
創(chuàng)建 index.js js文件
import Vue from 'vue';
import Confirm from './index.vue'; // 引入組件
let newInstance;
const ConfirmInstance = Vue.extend(Confirm); // 創(chuàng)建構(gòu)造函數(shù)
const initInstance = () => { // 執(zhí)行方法后完成掛載
newInstance = new ConfirmInstance(); // 實(shí)例化
document.body.appendChild(newInstance.$mount().$el);
// 實(shí)例化后手動(dòng)掛載,得到$el真實(shí)Dom,將其添加到body最后
}
export default options => {
//導(dǎo)出一個(gè)方法,接受配置參數(shù)
if (!newInstance) {
initInstance(); // 掛載
}
Object.assign(newInstance, options);
// 實(shí)例化后newInstance就是一個(gè)對(duì)象了,所以data內(nèi)的數(shù)據(jù)會(huì)
// 掛載到this下,傳入一個(gè)對(duì)象與之合并
return newInstance.show(vm => { // 顯示彈窗
newInstance = null; // 將實(shí)例對(duì)象清空
})
}esConfirm文件夾下創(chuàng)建組件 index.vue
<template>
<!-- 二次確認(rèn)彈窗 -->
<el-dialog width="440px" class="delete-confirm" :visible.sync="confirmDialigShow" :show-close="false"
:close-on-click-modal="false">
<div class="top">
<img class="danger" :src="require('./danger.png')">
<div class="title">{{ title }}</div>
</div>
<div class="bottom">{{ content }}</div>
<span slot="footer" class="dialog-footer">
<el-button v-if="showCancelButton" size="small" @click="cancel">{{ cancelButtonText || '取消' }}</el-button>
<el-button size="small" type="primary" @click="confirm">{{ confirmButtonText || '確定' }}</el-button>
</span>
</el-dialog>
</template>
<script>
export default {
data() {
return {
title: '提示',
content: '該數(shù)據(jù)刪除將無法恢復(fù),是否確認(rèn)刪除?',
confirmButtonText: '確定',
cancelButtonText: '取消',
showCancelButton: true,
confirmDialigShow: true
}
},
methods: {
show(cb) {
this.showFlag = true
typeof cb === "function" && cb.call(this, this)
return new Promise((resolve, reject) => {
this.reject = reject
this.resolve = resolve
})
},
cancel() {
this.reject("cancel")
this.hide()
},
confirm() {
this.resolve("confirm")
this.hide()
},
hide() {
this.showFlag = false
document.body.removeChild(this.$el)
this.$destroy()
}
}
}
</script>
<style lang='scss' scoped>
.delete-confirm {
// width: 440px;
box-shadow: 0px 4px 12px 0px rgba(0, 0, 0, 0.1);
border-radius: 4px;
.top {
display: flex;
align-items: center;
margin-bottom: 12px;
.danger {
width: 30px;
height: 24px;
margin-right: 12px;
}
.title {
font-weight: 500;
font-size: 16px;
color: rgba(0, 0, 0, 0.85);
line-height: 24px;
}
}
.bottom {
font-size: 14px;
font-weight: 400;
color: #324457;
line-height: 22px;
margin-top: 20px;
}
::v-deep .el-dialog__header {
display: none;
}
::v-deep {
.el-dialog {
margin-top: 20vh !important;
}
.el-dialog__body {
padding: 24px;
}
.el-dialog__footer {
border: none;
line-height: 0;
padding: 8px 24px 24px 0 !important;
}
}
}
</style>在main.js 注冊(cè)組件
import esConfirm from '@/components/esConfirm/index.js' Vue.prototype.$esConfirm = esConfirm
調(diào)用
this.$esConfirm({
title: '是否刪除?'
}).then(res => {
console.log(res)
}).catch(error => {
console.log(error)
})Vue中的信息提示this.$message方法的使用
代碼:
this.$message({
message:'保存成功了,我是message',
type: 'success'
})效果:

type的其他參數(shù):
success(成功) 、warning(警告)、info(消息)、error(錯(cuò)誤)
可以在請(qǐng)求成功之后執(zhí)行:
axios.post("http://localhost:3312/user/save",this.form).then((resp)=>{
let data = resp.data
if(data.success){
......
this.$message({
message:'保存成功了,我是message',
type: 'success'
})
}
})到此這篇關(guān)于vue實(shí)現(xiàn)原理this.$message的文章就介紹到這了,更多相關(guān)vue this.$message內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在vue中實(shí)現(xiàn)點(diǎn)擊選擇框阻止彈出層消失的方法
今天小編就為大家分享一篇在vue中實(shí)現(xiàn)點(diǎn)擊選擇框阻止彈出層消失的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue3-vue-router創(chuàng)建靜態(tài)路由和動(dòng)態(tài)路由方式
這篇文章主要介紹了vue3-vue-router創(chuàng)建靜態(tài)路由和動(dòng)態(tài)路由方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue項(xiàng)目如何實(shí)現(xiàn)rsa加密
這篇文章主要介紹了Vue項(xiàng)目如何實(shí)現(xiàn)rsa加密,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
vue3+ElementPlus使用lang="ts"報(bào)Unexpected?token錯(cuò)誤的解決
最近開發(fā)中遇到了些問題,跟大家分享下,這篇文章主要給大家介紹了關(guān)于vue3+ElementPlus使用lang="ts"報(bào)Unexpected?token錯(cuò)誤的解決辦法,需要的朋友可以參考下2023-01-01
vue文件上傳讀取文件數(shù)據(jù)進(jìn)行格式校驗(yàn)方式
該文章描述了使用Element-UI的`el-upload`組件實(shí)現(xiàn)文件上傳前的內(nèi)容校驗(yàn),通過`beforeUpload`鉤子讀取并解析文件內(nèi)容,校驗(yàn)其格式和數(shù)據(jù)有效性,確保符合要求后才進(jìn)行上傳,詳細(xì)介紹了校驗(yàn)邏輯及上傳流程2026-05-05
Vue學(xué)習(xí)筆記進(jìn)階篇之多元素及多組件過渡
本篇文章主要介紹了Vue學(xué)習(xí)筆記進(jìn)階篇之多元素及多組件過渡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
vue scroller返回頁(yè)面記住滾動(dòng)位置的實(shí)例代碼
這篇文章主要介紹了vue scroller返回頁(yè)面記住滾動(dòng)位置的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01

