I am trying to create custom decorators that returns a new instance of a class and this class must be created from an injected service which I can't figure out how to access it from the decorator function
Custom decorator
export function Collection(endpoint: string) {
return function (constructor: any) {
const mainService = CollectionModule.injector.get(MainService);
return mainService.getCollection(endpoint);
};
}
I want to access MainService
from my custom decorator which I tried using the module but the injector
property does not exist!
The service
@Injectable()
export class MainService {
config;
getCollection(endpoint: string): Collection {
return new Collection(endpoint, this.config);
}
}
Expected Usage
export class AppComponent implements OnInit {
@Collection('posts') postsCollection;
ngOnInit() {
console.log(this.postsCollection);
}
}
Update
Here is a stackblitz reproduction