/**
* 接口try 裝飾器
* @param loading 是否有l(wèi)oading效果
* @param cb 異常處理函數(shù)d 函數(shù)名
* @constructor
*/
export function Try(loading?: boolean, cb?: string) {
return function (target: object, key: string | symbol, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = async function (...args) {
try {
if (loading) {
Tips.showLoading();
}
return await method.call(this, ...args);
} catch (e) {
if (cb && target[cb]) {
target[cb](e);
} else {
Ehr.handleError(e);
}
} finally {
if (loading) {
Tips.hideLoading();
}
}
};
};
}
/**
* 節(jié)流裝飾器
* @param delay
* @constructor
*/
export function Throttle(delay = 300) {
let previous = 0;
return function (target: object, key: string | symbol, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = function (...args) {
const now = Date.now();
if (now - previous > delay) {
previous = now;
return method.call(this, ...args);
}
};
};
}
/**
* 防抖裝飾器
* @param delay
* @constructor
*/
export const Debounce = (delay: number = 200) => {
let timer: any = null;
return function (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = function (...args) {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
return method.call(this, ...args);
}, delay);
};
};
};
@Debounce()
@Try()
async login() {
if (this.isValid) return;
await LoginService.login();
await Service.becomeVip(this.userId);
YNavigator.redirectTo("/shop/pages/shopIndex/ShopIndexPage");
}
js裝飾器
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 隨著 ES6 和 TypeScript 中類的引入,使得我們在多個(gè)不同類之間共享或者擴(kuò)展一些方法或者行為的時(shí)候,變...