Vue.extend實現(xiàn)組件庫message組件示例詳解
概述
當(dāng)我們使用組件庫的時候,某些組件并不是直接放到模板當(dāng)中進行使用,而是通過api的方式調(diào)用生成組件并且掛在到我們的頁面中,其中最常見的就是message組件,我們在組件庫中看到的多數(shù)都是api調(diào)用的方式生成。記錄自己基本實現(xiàn)message組件。
Vue.extend
在vue中,要實現(xiàn)通過api方式實現(xiàn)組件的使用,這個aip是必不可少的,因此我們先了解下這個全局api的用法,官網(wǎng)說的很晦澀難懂,我的理解就是通過參數(shù)傳遞一個配置對象(這個配置對象就是我們模板中export default的那個對象,例如data,methods,props等等)都可以傳遞,接下來該函數(shù)會根據(jù)我們的配置對象生成一個繼承自Vue的子組件構(gòu)造函數(shù),然后我們就可以通過實例化構(gòu)造函數(shù),生成對應(yīng)的組件,然后我們就可以掛載到頁面了。
message 組件配置對象(就是.vue文件)
<template>
<div
:class="['message-box', 'message-box-' + type]"
:style="{ top: top + 'px' }"
>
<header>
<span class="close">X</span>
</header>
<div class="message--box-content">
{{ message }}
</div>
</div>
</template>
<script>
export default {
data() {
return {};
},
props: {
message: {
type: String,
default: "this is message box",
},
type: {
validator(value) {
return ["success", "error", "default"].indexOf(value) !== -1;
},
default: "default",
},
top: {
type: Number,
default: 20,
},
},
};
</script>
<style lang="less">
.message-box {
position: fixed;
left: 50%;
transform: translateX(-50%);
width: 400px;
height: 50px;
background-color: #ccc;
.close {
position: absolute;
top: 0;
right: 5px;
cursor: pointer;
}
.message--box-content {
line-height: 50px;
text-indent: 2em;
}
&.message-box-success {
background-color: green;
}
&.message-box-error {
background-color: red;
}
&.message-box-default {
background-color: #eee;
}
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
opacity: 0;
}
</style>
message 生成組件的函數(shù)
import MessageBoxOption from "./index.vue";
import Vue from "vue";
let messageBoxConstructor = Vue.extend({
...MessageBoxOption,
});
export default {
install(Vue) {
Vue.prototype.$Message = {
instanceList: [],
hide(types) {
for (let instance of this.instanceList) {
if (instance.types == types) {
instance &&
document.body.removeChild(instance.$el) &&
Reflect.deleteProperty(this, types);
}
}
},
success(message) {
if (!this.vmSuccess) {
let messageBox = new messageBoxConstructor({
propsData: {
message,
type: "success",
top: (this.instanceList.length + 1) * 55,
},
});
let $Message = messageBox.$mount();
this.vmSuccess = $Message;
this.instanceList.push({ ...$Message, types: "vmSuccess" });
document.body.appendChild($Message.$el);
setTimeout(() => {
this.hide("vmSuccess");
}, 4000);
}
},
error(message) {
if (!this.vmError) {
let messageBox = new messageBoxConstructor({
propsData: {
message,
type: "error",
top: (this.instanceList.length + 1) * 55,
},
});
let $Message = messageBox.$mount();
this.vmError = $Message;
this.instanceList.push({ ...$Message, types: "vmError" });
document.body.appendChild($Message.$el);
setTimeout(() => {
this.hide("vmError");
}, 6000);
}
},
};
},
};
使用方法
<template>
<div id="app">
<button @click="handleShowMessageBox">顯示</button>
<button @click="handleHideMessageBox">隱藏</button>
</div>
</template>
<script>
import MessageBox from "./components/MessageBox/index.vue";
export default {
name: "App",
components: { MessageBox },
methods: {
handleShowMessageBox() {
this.$Message.success("恭喜你,這是一條成功消息");
this.$Message.error("sorry,這是一條失敗消息");
},
handleHideMessageBox() {
this.$Message.hide();
},
},
};
</script>
效果圖

總結(jié)
- 要通過js的方式通過api調(diào)用生成,關(guān)鍵在在于Vue.extend函數(shù)的使用,上面只是個簡單版本的,可以根據(jù)自己的需要,自動加以擴展。
- 我們這種彈窗組件一般做成單例,因此點擊的時候不會重復(fù)出現(xiàn)相同類型的組件。
以上就是Vue.extend實現(xiàn)組件庫message組件示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Vue.extend message組件庫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue在取對象長度length時候出現(xiàn)undefined的解決
這篇文章主要介紹了vue在取對象長度length時候出現(xiàn)undefined的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
詳解Vue CLI3 多頁應(yīng)用實踐和源碼設(shè)計
這篇文章主要介紹了詳解Vue CLI3 多頁應(yīng)用實踐和源碼設(shè)計,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
wepy--用vantUI 實現(xiàn)上彈列表并選擇相應(yīng)的值操作
這篇文章主要介紹了wepy--用vantUI 實現(xiàn)上彈列表并選擇相應(yīng)的值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
深入探討Vue3實現(xiàn)多數(shù)據(jù)變化監(jiān)聽的方式
隨著 Vue 3 的發(fā)布,框架帶來了更多的新特性和增強,其中之一就是 watch 函數(shù)的升級,這一改進使得多個數(shù)據(jù)的變化偵聽更加優(yōu)雅和靈活,本文將深入探討 Vue 3 中的 watch 函數(shù),以及如何以更加優(yōu)雅的方式實現(xiàn)對多個數(shù)據(jù)變化的監(jiān)聽2023-08-08

