vue封裝全局彈窗警告組件this.$message.success問題
更新時間:2023年09月26日 09:11:49 作者:默默的小跟班
這篇文章主要介紹了vue封裝全局彈窗警告組件this.$message.success問題,具有很的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
先上效果圖。
背景有遮罩層,并且模糊,彈窗設置了最小寬度,文字超出范圍,彈窗會自動擴展。

寫一個彈窗組件
message.vue
<template>
<!-- 遮罩層 也是整個組件的容器-->
<div class="pop-container" v-if="isShow">
<div class="message-container">
<!-- 兩個icon放在一個容器中,但是只顯示一個 -->
<div class="icon-container">
<div class="icon-container-success" v-if="type === 'success'">
<!-- 引用了iview的Icon組件 -->
<Icon class="icon-checkmark" type="md-checkmark" size="30" color="#D8DCE9" />
</div>
<div class="icon-container-error" v-if="type === 'error'">
<Icon class="icon-close" type="md-close" size="30" color="#D8DCE9" />
</div>
</div>
<span class="message-text">{{ text }}</span>
</div>
</div>
</template><script>
export default {
name: 'message',
props: {
type: { // type控制是成功還是失敗
type: String,
default: 'success',
},
text: { // 彈窗的文字信息
type: String,
default: '',
},
isShow: { // 整個遮罩和彈窗是否顯示
type: Boolean,
default: true,
},
},
};
</script><style scoped lang="less">
.pop-container {
display: flex;
justify-content: center;
align-items: center;
z-index: 99999;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(24, 30, 53, 0.7);
backdrop-filter: blur(10px);
}
.message-container {
display: flex;
justify-content: center;
align-items: center;
min-width: 384px;
padding: 0 30px;
height: 170px;
background: #303e62;
box-shadow: 0px 0px 30px rgba(0, 0, 0, 0.1);
border-radius: 12px;
}
.icon-container {
position: relative;
height: 40px;
width: 40px;
border-radius: 50%;
&-error {
background-color: #fe1b1b;
height: 40px;
width: 40px;
border-radius: 50%;
.icon-close {
position: absolute;
right: 5px;
bottom: 5px;
font-weight: 900;
}
}
&-success {
background-color: #4ad991;
height: 40px;
width: 40px;
border-radius: 50%;
.icon-checkmark {
position: absolute;
right: 4px;
bottom: 5px;
font-weight: 900;
}
}
}
.message-text {
margin-left: 12px;
font-weight: 500;
font-size: 18px;
line-height: 27px;
}
</style>配置js文件
message.js
<script>
import vue from 'vue';
// 引入上面寫好的組件
import Message from './message';
// 全局警告彈窗 第一個參數(shù)為提示的文本信息,第二個參數(shù)可選,為彈窗持續(xù)時間
// 可以提前看一下知識點,Vue.extend + vm.$mount,可以讓我們在vue中用js一樣的方法去直接調用一個組件
// https://www.cnblogs.com/hentai-miao/p/10271652.html
// 將vue組件利用Vue.extend方法變成一個組件構造器,相當于一個類
const MsgClass = vue.extend(Message);
const MsgMain = {
show(text, type, duration) {
// 實例化這個組件
const instance = new MsgClass();
// 將組件實例掛在到一個元素上面,如果不傳參數(shù)就是掛載到body,或者也可以傳入其他已經(jīng)存在的元素的選擇器
instance.$mount(document.createElement('div'));
// 通過組件實例的$el屬性,可以訪問到這個組件元素,然后把它拼接到body上。
document.body.appendChild(instance.$el);
// 給這個實例傳入?yún)?shù)
instance.type = type;
instance.text = text;
instance.isShow = true;
// 設置一個延遲,過了時間彈窗消失
setTimeout(() => {
instance.isShow = false;
}, duration);
},
// 成功時調用這個方法
success(text, duration = 2000) {
this.show(text, 'success', duration);
},
// 失敗時調用這個方法
error(text, duration = 2000) {
this.show(text, 'error', duration);
},
};
// 全局注冊
function Msg() {
vue.prototype.$msg = MsgMain;
// 最終調用就是this.$msg.success() 或者this.$msg.error()
}
export default Msg;
</script>全局注冊組件
main.js
import Msg from '../src/components/message'; Vue.use(Msg);
使用
this.$msg.success('設置成功!'); // 第二個參數(shù)可以傳入彈窗持續(xù)時間,默認是2000
this.$msg.success('設置成功!',3000);
this.$msg.error('設置失敗!')總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue中@keyup.enter?@v-model.trim的用法小結
這篇文章主要介紹了Vue中@keyup.enter?@v-model.trim的用法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12

