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組件示例詳解的詳細內(nèi)容,更多關(guān)于Vue.extend message組件庫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Vue3中的watch偵聽器和watchEffect高級偵聽器
這篇文章主要介紹了Vue3中的watch偵聽器和watchEffect高級偵聽器,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
vue3+ts+vant移動端H5項目搭建的實現(xiàn)步驟
本文主要介紹了vue3+ts+vant移動端H5項目搭建,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
vue登錄頁實現(xiàn)使用cookie記住7天密碼功能的方法
這篇文章主要介紹了vue登錄頁實現(xiàn)使用cookie記住7天密碼功能的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
vue-baidu-map 進入頁面自動定位的解決方案(推薦)
這篇文章主要介紹了vue-baidu-map 進入頁面自動定位的解決方案,需要的朋友可以參考下2018-04-04

