As per how typescript design works ,if i write a decorator like,
class foo{
@fooDecorator
public fooMethod(){
}
}
it gives the transpiled javascript as
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var foo = (function () {
function foo() {
}
foo.prototype.fooMethod = function () {
};
__decorate([
fooDecorator
], foo.prototype, "fooMethod", null);
return foo;
}());
as per there transpiled code whenever my app gets bootstrapped the decorator function get executed, but how i want is whenever the underlying function "fooMethod" is executed the decorator method "fooDecorator" should execute, how can i attain the same, please help me with the issue
Decorators provide a way to add both annotations and a meta-programming syntax for class declarations and members. Decorators are a stage 2 proposal for JavaScript and are available as an experimental feature of TypeScript. NOTE Decorators are an experimental feature that may change in future releases.
As of writing this guide, decorators are still an experimental feature in TypeScript. To enable this feature, set the experimentalDecorators compiler flag either on the command line or in your tsconfig. json file.
target: Constructor function of the class if we used decorator on the static member, or prototype of the class if we used decorator on instance member. In our case it is firstMessage which is an instance member, so the target will refer to the prototype of the Greeter class. propertyKey: It is the name of the property.
A Decorator is a special kind of declaration that can be applied to classes, methods, accessor, property, or parameter.
You can do it like this. In the decorator replace your original method with 'wrapper' that will first call the supplied method that should be called each time and then call the original one.
The code below illustrate this approach:
Your decorator:
export function Foo(funcToCallEveryTime: (...args: any[]) => void)
{
return (target: any, key: string, descriptor: any) =>
{
var originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
funcToCallEveryTime(...args);
return originalMethod.apply(target, args);
}
return descriptor;
}
}
And method you decorate with @Foo decorator like this:
@Foo((...args: any[]) => { console.log("Before method call:", args[0]); })
private TestMethod(param: string): void
{
console.log("Method call");
//...
}
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With