It is possible generate a service with angular cli and add it as a provider in the app.module.ts in a single step or using an special option in the ng g service command?
When a execute:
$ ng g service services/backendApi
installing service
create src/app/services/backend-api.service.spec.ts
create src/app/services/backend-api.service.ts
WARNING Service is generated but not provided, it must be provided to be used
Next to it, (and according to the WARNING message) I usually add it to provider section on app.module.ts using the text editor:
@NgModule({
declarations: [
AppComponent,
...
],
imports: [
....
],
providers: [BackendApiService],
bootstrap: [AppComponent]
})
It is possible to do it with a single step, to automatize this?
This question is related to
javascript
typescript
angular-cli
In Command prompt go to project folder and execute following:
ng g s servicename
Add a service to the Angular 4 app using Angular CLI
An Angular 2 service is simply a javascript function along with it's associated properties and methods, that can be included (via dependency injection) into Angular 2 components.
To add a new Angular 4 service to the app, use the command ng g service serviceName
. On creation of the service, the Angular CLI shows an error:
To solve this, we need to provide the service reference to the src\app\app.module.ts
inside providers
input of @NgModule
method.
Initially, the default code in the service is:
import { Injectable } from '@angular/core';
@Injectable()
export class ServiceNameService {
constructor() { }
}
A service has to have a few public methods.
In Angular 5.12 and latest Angular CLI, do
ng generate service my-service -m app.module
Specify paths
--app
--one
one.module.ts
--services
--two
two.module.ts
--services
Create Service with new folder in module ONE
ng g service one/services/myNewServiceFolderName/serviceOne --module one/one
--one
one.module.ts // service imported and added to providers.
--services
--myNewServiceFolderName
serviceOne.service.ts
serviceOne.service.spec.ts
run the below code in Terminal
makesure You are inside your project folder in terminal
ng g s servicename --module=app.module
slight change in syntax from the accepted answer for Angular 5 and angular-cli 1.7.0
ng g service backendApi --module=app.module
The recommended approach for a singleton service for Angular 6 and beyond is :
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class UserService {
}
In fact the CLI --module
switch doesn't even exist any more for registering a service at the app
level because it doesn't need to modify app.module.ts
anymore.
This will create the above code, without needing to specify a module.
ng g s services/user
So if you don't want your service to be a singleton you must remove the providedIn
code yourself - and then add it manually to providers
for a component or lazy loaded module. Doesn't look like there is currently a switch to not generate the providedIn: 'root'
part so you need to manually remove it.
Source: Stackoverflow.com