1 回答

TA貢獻1839條經(jīng)驗 獲得超15個贊
你快到了。你之所以得到這個,是undefined因為this你的裝飾器中是未定義的,應(yīng)該引用你的目標(biāo)的實例化實例。將該定義更改為常規(guī)函數(shù)調(diào)用而不是箭頭函數(shù)應(yīng)該可以解決問題。
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);
};
}
}
};
};
添加回答
舉報