I have created a custom decorator for class methods and it depends upon injected services of the class of the decorated method. The injectables that this decorator relies upon are userService and authService.
Here is the decorator:
export function authenticated(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor) {
let originalMethod = descriptor.value;
descriptor.value = function(...args: any[]) {
if (!this.userService || !this.authService) {
throw Error("Class must import userService and authService");
}
if (!this.userService.isAuthenticated$.value) {
return this.authService.openLoginSignup();
}
return originalMethod.apply(this, args);
};
}
I implemented a check to confirm that the services are imported into the parent class correctly, but the problem with it is that it only gets called when the decorated method gets called.
Is there any way to inject angular 2 injectables into decorators or other non-components?