1 回答

TA貢獻1839條經驗 獲得超15個贊
你快到了。你之所以得到這個,是undefined因為this你的裝飾器中是未定義的,應該引用你的目標的實例化實例。將該定義更改為常規函數調用而不是箭頭函數應該可以解決問題。
export const ensureCall = (method: string) => {
return (target: any) => {
for (const prop of Object.getOwnPropertyNames(target.prototype)) {
if (prop === method || prop === 'constructor') continue;
const originalMethod = target.prototype[prop];
if (originalMethod instanceof Function) {
// Regular function declaration
target.prototype[prop] = async function(...args: any[]) {
await target.prototype[method]();
// Now `this` refers to an instantiated instance
return originalMethod.apply(this, args);
};
}
}
};
};
添加回答
舉報