装饰器解决按钮、表单重复提交

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
export function HandleAsyncOperate(name?) {
return function (
target: any,
key: string,
descriptor: TypedPropertyDescriptor<Function>
) {
let func = descriptor.value;
let flag = false;
console.log(arguments, "HandleFormSubmit");
descriptor.value = async function (...args) {
console.log(flag, 'before func.call')
if (flag) {
console.log('flag true return false')
return false;
}
flag = true;
await func.call(this, ...args);
flag = false;
console.log(flag, 'after func.call')
};
};
}
// 方式一
@HandleAsyncOperate()
async asyncOpertate() {
await new Ajax(...)
}
// 方式二
@HandleAsyncOperate()
asyncOpertate() {
return new Promise(...)
}
坚持原创技术分享,您的支持将鼓励我继续创作!