Hi i am trying to make a class decorator for my server services so that i can share my services with ease on any component, I have a func called 'onUser' that is acting as a callback when ever i get the user data from the server but trying to access 'this' to the callbach that i call on the decorator shows that 'this' is different to the 'this' of the container component what am i missing? thanks
export function UserSubscriber() {
return (constructor: any) => {
const component = constructor.name;
const userService: UserClientService =
InjectorInstance.get<UserClientService>(UserClientService);
let subscription: Subscription;
subscription = userService.user$.subscribe(function(user) {
constructor.prototype.onUser(user);
});
const orgOnInit = constructor.prototype['ngOnInit'];
constructor.prototype['ngOnInit'] = function (...args) {
if (orgOnInit) {
orgOnInit.apply(this, args);
}
};
const orgOnDestroy = constructor.prototype['ngOnDestroy'];
constructor.prototype['ngOnDestroy'] = function(...args) {
subscription.unsubscribe();
if (orgOnDestroy) {
orgOnDestroy.apply(this, args);
}
};
};
}
@UserSubscriber()
@Component({
...
})
export class AppComponent {
...
onUser(user) {
console.log(user);
console.log(this); // this is not the instance of this component
}
}