Vue消息提示this.$message方法使用解讀
Vue消息提示this.$message方法
HTML
<el-button @click="saveData">彈窗</el-button>
JavaScript
saveData(){
? ? this.$message({undefined
? ? ? ? message:“這是彈框消息”,
? ? ? ? type:‘success'
? ? ?})
?// type 取值 ‘success'(成功) /warning(警告)/info(消息)/error(錯(cuò)誤)/
}element 的 this.$message( ) 消息提示實(shí)現(xiàn)
在vue項(xiàng)目中,直接通過(guò)js代碼 this.$message( )就可以調(diào)出消息提示組件,這是如何實(shí)現(xiàn)的呢
主要分為以下幾步
1.用 Vue.extend 創(chuàng)建組件的模板(構(gòu)造函數(shù))
2.創(chuàng)建一個(gè)函數(shù),在函數(shù)內(nèi)部, 實(shí)例化組件并進(jìn)行掛載到相應(yīng)元素上
3.將創(chuàng)建的這個(gè)函數(shù)綁定到Vue原型上,就可以通過(guò)this .訪問(wèn)了
上代碼,
如下目錄結(jié)構(gòu)

在main.js中
import Vue from "vue";
import message from "./main.vue";
// 1.用 Vue.extend 創(chuàng)建組件的模板(構(gòu)造函數(shù))
let messageConstructor = Vue.extend(message);
let instance;
const Message = function(options) {// options是傳入的參數(shù)配置 {message: '成功',type: "success"offset: 80}
// 2.實(shí)例化組件
instance = new messageConstructor({ data: options }); // 把options導(dǎo)入data中
// 3.組件掛載
instance.$mount();
document.body.appendChild(instance.$el);
// 設(shè)置offset
let verticalOffset = options.offset || 20;
instance.verticalOffset = verticalOffset;
instance.duration = options.duration || 3000;
return instance;
};
export default Message;在main.vue中
<template>
<div v-show="visible"
:class="['my-message', 'my-message-' + type, center ? 'is-center' : '']"
:style="positionStyle"
>
<slot>
<p>{{ message }}</p>
</slot>
<i v-if="showClose" class="el-icon-close" @click="close"></i>
</div>
</template>
<script>
export default {
name: "Message",
data() {
return {
visible:false,
verticalOffset: 20,
duration: 3000,
timer: null,
center: false,
showClose: false,
closed: false,
};
},
mounted() {
this.autoClose();
},
beforeDestroy() {
clearTimeout(this.timer);
},
watch: {
closed(newVal) {
if (newVal) {
this.visible = false;
}
}
},
computed: {
positionStyle() {
return {
top: `${this.verticalOffset}px`,
};
},
},
methods: {
autoClose() {
this.timer = setTimeout(() => {
this.$destroy(true);
this.$el.parentNode.removeChild(this.$el);
}, this.duration);
},
close() {
this.closed = true;
clearTimeout(this.timer);
}
},
};
</script>
<style>
.my-message {
min-width: 380px;
border: 1px solid #ebeef5;
border-radius: 4px;
position: fixed;
left: 50%;
top: 20px;
transform: translateX(-50%);
background-color: #edf2fc;
transition: opacity 0.3s, transform 0.4s, top 0.4s;
overflow: hidden;
display: flex;
align-items: center;
padding: 0 15px;
}
p {
}
.my-message-info {
color: #909399;
}
.my-message-success {
background: #f2f9ec;
color: #67c23a;
border-color: #e4f2da;
}
.my-message-warning {
background: #fcf6ed;
color: #e6a23c;
border-color: #f8ecda;
}
.my-message-error {
background: #fcf0f0;
color: #f56c6c;
border-color: #f9e3e2;
}
.is-center {
justify-content: center;
}
</style>在index.js中
import Vue from "vue"; import Message from './src/main' Vue.prototype.$message = Message
ok了,大晚上的有點(diǎn)困,有些地方有些潦草,有時(shí)間再改下。。。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3超詳細(xì)的ref()用法教程(看這一篇就夠了!)
這篇文章主要給大家介紹了關(guān)于Vue3超詳細(xì)的ref()用法的相關(guān)資料,在Vue3中ref函數(shù)不僅可以用于在組件中獲取DOM元素或子組件的引用,還可以直接訪問(wèn)組件元素本身,需要的朋友可以參考下2023-07-07
vue3 element-plus二次封裝組件系列之伸縮菜單制作
這篇文章主要介紹了vue3 element-plus二次封裝組件系列之伸縮菜單制作,是基于vue3 vite element-plus搭建的,值的注意的時(shí)候,里面的圖標(biāo)組件是經(jīng)過(guò)處理的,結(jié)合實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
VUE2舊項(xiàng)目重新安裝依賴后@vue/compiler-sfc編譯報(bào)錯(cuò)問(wèn)題
在VUE2舊項(xiàng)目中重新安裝依賴后,如果遇到@vue/compiler-sfc編譯報(bào)錯(cuò),首先需要檢查package.json文件中的Vue版本是否升級(jí)到2.7版本,2.7版本的編譯插件不再支持/deep/這種樣式穿透,版本^和~的區(qū)別在于^只能鎖住第一位數(shù)2025-01-01
vue組件實(shí)現(xiàn)文字居中對(duì)齊的方法
這篇文章主要為大家詳細(xì)介紹了vue組件實(shí)現(xiàn)文字居中對(duì)齊的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
基于vue+echarts 數(shù)據(jù)可視化大屏展示的方法示例
這篇文章主要介紹了基于vue+echarts 數(shù)據(jù)可視化大屏展示的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2020-03-03

