Vue.extend實(shí)現(xiàn)組件庫(kù)message組件示例詳解
概述
當(dāng)我們使用組件庫(kù)的時(shí)候,某些組件并不是直接放到模板當(dāng)中進(jìn)行使用,而是通過(guò)api的方式調(diào)用生成組件并且掛在到我們的頁(yè)面中,其中最常見(jiàn)的就是message組件,我們?cè)诮M件庫(kù)中看到的多數(shù)都是api調(diào)用的方式生成。記錄自己基本實(shí)現(xiàn)message組件。
Vue.extend
在vue中,要實(shí)現(xiàn)通過(guò)api方式實(shí)現(xiàn)組件的使用,這個(gè)aip是必不可少的,因此我們先了解下這個(gè)全局api的用法,官網(wǎng)說(shuō)的很晦澀難懂,我的理解就是通過(guò)參數(shù)傳遞一個(gè)配置對(duì)象(這個(gè)配置對(duì)象就是我們模板中export default的那個(gè)對(duì)象,例如data,methods,props等等)都可以傳遞,接下來(lái)該函數(shù)會(huì)根據(jù)我們的配置對(duì)象生成一個(gè)繼承自Vue的子組件構(gòu)造函數(shù),然后我們就可以通過(guò)實(shí)例化構(gòu)造函數(shù),生成對(duì)應(yīng)的組件,然后我們就可以掛載到頁(yè)面了。
message 組件配置對(duì)象(就是.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é)
- 要通過(guò)js的方式通過(guò)api調(diào)用生成,關(guān)鍵在在于Vue.extend函數(shù)的使用,上面只是個(gè)簡(jiǎn)單版本的,可以根據(jù)自己的需要,自動(dòng)加以擴(kuò)展。
- 我們這種彈窗組件一般做成單例,因此點(diǎn)擊的時(shí)候不會(huì)重復(fù)出現(xiàn)相同類(lèi)型的組件。
以上就是Vue.extend實(shí)現(xiàn)組件庫(kù)message組件示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Vue.extend message組件庫(kù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Vue3中的watch偵聽(tīng)器和watchEffect高級(jí)偵聽(tīng)器
這篇文章主要介紹了Vue3中的watch偵聽(tīng)器和watchEffect高級(jí)偵聽(tīng)器,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
簡(jiǎn)單方法實(shí)現(xiàn)Vue?無(wú)限滾動(dòng)組件示例
這篇文章主要為大家介紹了簡(jiǎn)單方法實(shí)現(xiàn)Vue?無(wú)限滾動(dòng)組件示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
vue3+ts+vant移動(dòng)端H5項(xiàng)目搭建的實(shí)現(xiàn)步驟
本文主要介紹了vue3+ts+vant移動(dòng)端H5項(xiàng)目搭建,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
vue登錄頁(yè)實(shí)現(xiàn)使用cookie記住7天密碼功能的方法
這篇文章主要介紹了vue登錄頁(yè)實(shí)現(xiàn)使用cookie記住7天密碼功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
vue-baidu-map 進(jìn)入頁(yè)面自動(dòng)定位的解決方案(推薦)
這篇文章主要介紹了vue-baidu-map 進(jìn)入頁(yè)面自動(dòng)定位的解決方案,需要的朋友可以參考下2018-04-04

