I'm creating a service for an Angular (2+) application and all the documentation uses classes, but I prefer to write the service as a function.
Here's what I want to work (but doesn't):
import { Injectable } from '@angular/core';
export const AframeMessengerService = Injectable()(function AframeMessengerService() {
console.log('aframe messenger');
});
With this, I get this error in the file which injects it:
Cannot find name 'AframeMessengerService'.
Here's what does work, but is not what I want:
import { Injectable } from '@angular/core';
@Injectable()
export class AframeMessengerService {
constructor() {
console.log('aframe messenger');
}
}
Statements that look like @SomeName are decorators. Decorators are a proposed extension to JavaScript. In short, decorators let programmers modify and/or tag methods, classes, properties and parameters. In this section the focus will be on decorators relevant to DI: @Inject and @Injectable .
We use the @Inject parameter decorator to instruct Angular we want to resolve a token and inject a dependency into a constructor. We use the @Injectable class decorators to automatically resolve and inject all the parameters of class constructor.
The @Injectable() decorator defines a class as a service in Angular and allows Angular to inject it into a component as a dependency. Likewise, the @Injectable() decorator indicates that a component, class, pipe, or NgModule has a dependency on a service. The injector is the main mechanism.
Marking a class with @Injectable ensures that the compiler will generate the necessary metadata to create the class's dependencies when the class is injected.
Basically you're doing everything correct here:
import { Injectable } from '@angular/core';
export const AframeMessengerService = Injectable()(function AframeMessengerService() {
console.log('aframe messenger');
});
Now you need to add this to module providers:
import { AframeMessengerService } from './a.service';
@NgModule({
...
providers: [AframeMessengerService],
})
export class AppModule {}
And inject it like this:
import { AframeMessengerService } from './a.service';
@Component({
selector: 'my-app',
...
})
export class AppComponent {
constructor(@Inject(AframeMessengerService) s) { }
One thing to note is that when you will be injecting services into AframeMessengerService
you need to pass an index of parameter:
import { Inject, Injectable, Injector } from '@angular/core';
const AframeMessengerService = Injectable()(function AframeMessengerService(i) {
console.log('aframe messenger');
});
Inject(Injector)(AframeMessengerService, null, 0);
^^^^^^^
export { AframeMessengerService };
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