vue如何自定義封裝API組件
自定義封裝API組件
1.創(chuàng)建vue組件
<template>
?? ?<div >
?? ??? ?<div class="alert">
? ? ? ? <div class="alert-main" v-for="item in notices" :key="item.name">
? ? ? ? ? ? <div class="alert-content">{{ item.content }}</div>
? ? ? ? </div>
? ? </div>
?? ?</div >
</template><script>
? ?//多個(gè)提示框命名
? ? let seed = 0;
? ? function getUuid() {
? ? ? ? return 'alert_' + (seed++);
? ? }
?? ?export default {
?? ??? ?data() {
?? ??? ??? ?return {
? ? ? ? ? ? ? ? notices: []//多個(gè)提示框保存至數(shù)組
?? ??? ??? ?}
?? ??? ?},
?? ??? ?methods:{
?? ??? ? ?add(notice) {
? ? ? ? ? ? ? ? const name = getUuid();//獲取當(dāng)前提示框名稱(chēng)
? ? ? ? ? ? ? ? //Object.assign 淺拷貝不會(huì)跳過(guò)那些值為 [null] 或 [undefined]的源對(duì)象
? ? ? ? ? ? ? ? // Object.assign(target, ...sources);target: 目標(biāo)對(duì)象,sources:源對(duì)象
? ? ? ? ? ? ? ? let _notice = Object.assign({
? ? ? ? ? ? ? ? ? ? name: name
? ? ? ? ? ? ? ? }, notice);
? ? ? ? ? ? ? ? //將當(dāng)前提示框標(biāo)簽保存到notices
? ? ? ? ? ? ? ? this.notices.push(_notice);
? ? ? ? ? ? ? ? // 定時(shí)移除,單位:秒
? ? ? ? ? ? ? ? const time= notice.time|| 1.5;
? ? ? ? ? ? ? ? //1.5s后調(diào)用移除方法
? ? ? ? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? ? ? ? ? this.remove(name);
? ? ? ? ? ? ? ? }, time* 1000);
? ? ? ? ? ?},
? ? ? ? ? remove(name) {
? ? ? ? ? ? ? ? const notices = this.notices;
? ? ? ? ? ? ? ? for (let i = 0; i < notices.length; i++) {
? ? ? ? ? ? ? ? ? ? if (notices[i].name === name) {
? ? ? ? ? ? ? ? ? ? ? ? this.notices.splice(i, 1);
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ?}
?? ?}
</script><style lang="scss"> </style>
2.創(chuàng)建Alter.js生成組件
import Vue from 'vue'
import Alter from '/components/Alter/Alter.vue'
//Alter添加新屬性,newInstance是個(gè)函數(shù)需求參數(shù)為properties
Alter.newInstance=properties=>{
?? ?const props=properties || {};
?? ?//創(chuàng)建一個(gè)Vue組件對(duì)象
?? ?const Instance=new Vue({
?? ??? ?data:props,
?? ??? ?render(h){
?? ??? ??? ?return h(Alter,{
?? ??? ??? ??? ?props:props
?? ??? ??? ?})
?? ??? ?}
?? ?});
?? ?//等待接口調(diào)用的時(shí)候在實(shí)例化組件,避免進(jìn)入頁(yè)面就直接掛載到body上
?? ?const component=Instance.$mount();
?? ?//實(shí)例化一個(gè)組件,然后掛載到body上
?? ?document.body.appendChild(component.$el);
?? ?//通過(guò)閉包維護(hù)alter組件的引用
?? ?const alter=Instance.$children[0];
?? ?return{
?? ??? ?//Alter組件對(duì)外暴露的兩個(gè)方法
?? ??? ?add(noticeProps){
?? ??? ??? ?alter.add(noticeProps);
?? ??? ?},
?? ??? ?remove(name){
?? ??? ??? ?alter.remove(name);
?? ??? ?}
?? ?}
}
//提示單例
let messageInstance;
function getMessageInstance(){
?? ?messageInstance=messageInstance || Alter.newInstance();
?? ?return messageInstance;
}
//定義函數(shù)實(shí)現(xiàn)
function notice({time='',content=''}){
?? ?let instance=getMessageInstance();
?? ?instance.add({
?? ??? ?time:1.5,
?? ??? ?content:''
?? ?})
}
//公布方法
export default{
?? ?info(options){
?? ??? ?return notice(options);
?? ?}
}3.導(dǎo)入Vue
import alert from './alert.js'
// 掛載到Vue原型上
Vue.prototype.$Alert = alert
// 然后在組件中使用
this.$Alert.info({time: 1.5, content: '提示'})如何封裝使用api形式調(diào)用的vue組件
在實(shí)際開(kāi)發(fā)中一般有兩種封裝vue組件的方法:一種就是常用的的通過(guò)props父組件傳值給子組件的方法:
子組件

父組件

還有一種就是通過(guò)調(diào)用api的形式,下面例子是本人在實(shí)際項(xiàng)目中封裝的一個(gè)自定義圖標(biāo)的彈窗組件
首先實(shí)現(xiàn)組件的UI頁(yè)面(css部分截圖不完整)

在vue文件的同目錄下新建alertTips.js文件

頁(yè)面中調(diào)用方法:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3響應(yīng)式方案及ref?reactive的區(qū)別詳解
眾所周知ref和reactive都是用來(lái)作響應(yīng)式數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Vue3響應(yīng)式方案及ref?reactive區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
使用webpack打包后的vue項(xiàng)目如何正確運(yùn)行(express)
這篇文章主要介紹了使用webpack打包后的vue項(xiàng)目如何正確運(yùn)行(express) ,接下來(lái)通過(guò)本文給大家詳細(xì)介紹,需要的朋友可以參考下2018-10-10
如何使用vue-pdf-embed實(shí)現(xiàn)PDF在線預(yù)覽
vue-pdf-embed是一個(gè)基于Vue.js的插件,專(zhuān)門(mén)用于在Vue應(yīng)用中嵌入和展示PDF文件,本文將使用vue-pdf-embed實(shí)現(xiàn)PDF在線預(yù)覽功能,有需要的小伙伴可以參考一下2025-03-03
vue自定義底部導(dǎo)航欄Tabbar的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue自定義底部導(dǎo)航欄Tabbar的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
vue3+elementUI實(shí)現(xiàn)懸浮多行文本輸入框效果
這篇文章主要為大家詳細(xì)介紹了vue3如何引用elementUI實(shí)現(xiàn)懸浮文本輸入框效果,以便實(shí)現(xiàn)多行文本輸入,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10
Vue 項(xiàng)目中如何使用fullcalendar 時(shí)間段選擇插件(類(lèi)似課程表格)
最近完成一個(gè)項(xiàng)目,需要選擇一個(gè)會(huì)議室,但是最好能夠通過(guò)在圖上顯示出該 會(huì)議室在某某時(shí)間段內(nèi)已經(jīng)被預(yù)定了,初看這個(gè)功能感覺(jué)很棘手,仔細(xì)分析下實(shí)現(xiàn)起來(lái)還是挺容易的,下面通過(guò)示例代碼講解Vue項(xiàng)目中使用fullcalendar時(shí)間段選擇插件,感興趣的朋友一起看看吧2024-07-07

