日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
活用async/await,讓Vue變得更好用的裝飾器!

下文三個(gè)裝飾器,都是利用了async/await把異步變成同步的特性實(shí)現(xiàn)的。

要求被裝飾的方法必須寫(xiě)成async/await,用起來(lái)十分方便,實(shí)現(xiàn)徹底被隱藏在了裝飾器內(nèi)部。

前兩個(gè)都是用在ts環(huán)境下class寫(xiě)法的vue里的。不過(guò)看清楚邏輯后,很容易修改成可以用在js環(huán)境中的vue組件上。

1. 給vue添加一個(gè)指示初始化完成的變量。

指業(yè)務(wù)相關(guān)的初始化邏輯都完成了 比如搜索功能:搜索中顯示loading,結(jié)果為空時(shí)顯示暫無(wú)數(shù)據(jù)。但是第一次打開(kāi)頁(yè)面時(shí),搜索還沒(méi)完成,但顯示暫無(wú)數(shù)據(jù)又不合適 這個(gè)時(shí)候就需要一個(gè)這樣的變量處理邊界情況 用于ts環(huán)境下的vue。

通過(guò)裝飾器添加這個(gè)屬性,并包裝vue的created, mounted和beforeDestroy方法。當(dāng)created或者mounted里發(fā)出的請(qǐng)求完成后,就把pageIsReady設(shè)為true。使用這個(gè)裝飾器時(shí),在業(yè)務(wù)代碼中完全無(wú)感,沒(méi)有任何心智負(fù)擔(dān)。 

 
 
 
  1. import { Constructor } from"vue/types/options";  
  2. export type WrapReadyProperty = T & { pageIsReady?: boolean }  
  3. /**  
  4.  * 在@compontent 之后使用這個(gè)裝飾器,  
  5.  * 組件就會(huì)被注入屬性 pageIsReady,  
  6.  * 當(dāng)created和mounted都執(zhí)行完成時(shí) pageIsReady 變成true, 
  7.  * 要求mounted或created是async/await。(取決于在哪個(gè)方法中發(fā)請(qǐng)求初始化組件)  
  8.  * 然后可以在template中直接使用。  
  9.  * 在script中使用調(diào)用isPageReady.call(this)方法;  
  10.     */  
  11. exportdefaultfunction PageReadyStatus() {  
  12.     let createdDone = false;  
  13.     let mountedDone = false;  
  14.     function isCreatedMountedAllDone() {  
  15.         return createdDone && mountedDone;  
  16.     }  
  17.     returnfunction pageReadyEnhancement(target: T) {  
  18.         const oldMounted = target.prototype.mounted || function() { }  
  19.         const oldCreated = target.prototype.created || function() { }  
  20.         const oldBeforeDestroy = target.prototype.beforeDestroy || function() { }  
  21.         target.prototype.pageIsReady = false;  
  22.         target.prototype.created = asyncfunction(...params: any[]) {  
  23.             await oldCreated.apply(this, params);  
  24.             createdDone = true;  
  25.             this.pageIsReady = isCreatedMountedAllDone()  
  26.         }  
  27.         target.prototype.mounted = asyncfunction(...params: any[]) {  
  28.             await oldMounted.apply(this, params); 
  29.             mountedDone = true;  
  30.             this.pageIsReady = isCreatedMountedAllDone()  
  31.         }  
  32.         target.prototype.beforeDestroy = asyncfunction(...params: any[]) {  
  33.             await oldBeforeDestroy.apply(this, params);  
  34.             mountedDone = false;  
  35.             createdDone = false;  
  36.             this.pageIsReady = false;  
  37.         }  
  38.         return target  
  39.     };  
  40. }  
  41. exportfunction isPageReady(this: WrapReadyProperty) {  
  42.     returnthis.pageIsReady  

2. 給事件回調(diào)函數(shù)和按鈕Dom添加防抖與loading樣式

用于ts環(huán)境下的vue。

通過(guò)裝飾器包裝被裝飾的方法。要求被包裝的方式是async/await的。這樣裝飾器內(nèi)只需要用一個(gè)await就可以得知被包裝的方法是否執(zhí)行完成。同時(shí),可以從事件對(duì)象中拿到被點(diǎn)擊的dom元素并修改它。 

 
 
 
  1. /*  
  2.  * 請(qǐng)保證被包裝的方法的參數(shù)列表最后一個(gè)是點(diǎn)擊事件的參數(shù)  
  3.  */  
  4. exportdefaultfunction buttonThrottle() {  
  5.     let pending = false;  
  6.     returnfunction(target: any, name: string): any {  
  7.         const btnClickFunc = target[name];  
  8.         const newFunc = asyncfunction(this: Vue, ...params: any[]) {  
  9.             if (pending) {  
  10.                 return;  
  11.             }  
  12.             const event:Event = params[params.length - 1];  
  13.             let btn = event.target as HTMLElement  
  14.             pending = true;  
  15.             const recoverCursor = changeCursor(btn);  
  16.             try {  
  17.                 await btnClickFunc.apply(this, params);  
  18.             } catch (error) {  
  19.                 console.error(error);  
  20.             }  
  21.             recoverCursor();  
  22.             pending = false;  
  23.         };  
  24.         target[name] = newFunc;  
  25.         return target;  
  26.     };  
  27. }  
  28. function changeCursor(btn?: HTMLElement) {  
  29.     if (btn == null) {  
  30.         return() => {};  
  31.     }  
  32.     const oldCursor = btn.style.cursor;  
  33.     btn.style.cursor = "wait";  
  34.     return() => {  
  35.         btn.style.cursor = oldCursor;  
  36.     };  

用法: 在點(diǎn)擊事件函數(shù)上使用這個(gè)裝飾器。裝飾器會(huì)自動(dòng)檢測(cè)該函數(shù)是否執(zhí)行完成,并在執(zhí)行過(guò)程中往按鈕的Dom節(jié)點(diǎn)上添加point:wait屬性。

 
 
 
  1. import { Component, Vue } from"vue-property-decorator";  
  2.     import buttonThrottle from"@/ui/view/utils/buttonThrottle";  
  3.     type Member = { account_no: string; name: string; warn?: string };  
  4.     @Component({ components: {} })  
  5.     exportdefaultclass AddMemberInput extends Vue {        @buttonThrottle()  
  6.         private async confirmAdd() {  
  7.             awaitthis.addMembers(this.getVaildMembers());  
  8.         }  
  9.     } 

3. mounted之前顯示白屏

用于js的vue中包裝vue的對(duì)象。

同上,通過(guò)async/await獲得mounted或者created是否執(zhí)行完成 再通過(guò)指向vue實(shí)力的this拿到組件根節(jié)點(diǎn),然后按需修改它 以下代碼只是將組件隱藏了,實(shí)際上可以寫(xiě)更復(fù)雜的邏輯,在加載過(guò)程中顯示其他內(nèi)容,畢竟拿到了Dom,想干嘛就干嘛。 

 
 
 
  1. function firstPaintControl(vueObj) {  
  2.     let oldMounted = vueObj.mounted || function() {};  
  3.     vueObj.mounted = asyncfunction(...params) {  
  4.       this.$el.style.visibility = 'hidden';  
  5.       await oldMounted.apply(this, params);  
  6.       this.$el.style.visibility = 'visible';  
  7.     };  
  8.     return vueObj;  
  9.   }  

本文題目:活用async/await,讓Vue變得更好用的裝飾器!
文章位置:http://www.5511xx.com/article/dhopiii.html