admin管理员组文章数量:1134248
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?
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?
Share Improve this question edited Aug 14, 2017 at 19:46 Xameer 31.2k27 gold badges146 silver badges229 bronze badges asked Mar 12, 2017 at 14:44 Pablo Ezequiel InchaustiPablo Ezequiel Inchausti 20.4k7 gold badges30 silver badges44 bronze badges10 Answers
Reset to default 119Actually, it is possible to provide the service (or guard, since that also needs to be provided) when creating the service.
The command is the following...
ng g s services/backendApi --module=app.module
Edit
It is possible to provide to a feature module, as well, you must give it the path to the module you would like.
ng g s services/backendApi --module=services/services.module
Angular 6+ Singleton Services
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.
Looks like in Angular v11 and higher, we don't have option "s" anymore:
ng g service services/backendApi
or like that:
ng g service services/backendApi --flat --skipTests=true
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
slight change in syntax from the accepted answer for Angular 5 and angular-cli 1.7.0
ng g service backendApi --module=app.module
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
ng generate service userService
//shorthand for creating service
ng g s serviceName
** When you fire this command it will automatically registered inside providers array. **
providers: [
UserService
]
In Command prompt go to project folder and execute following:
ng g s servicename
run the below code in Terminal
makesure You are inside your project folder in terminal
ng g s servicename --module=app.module
本文标签: javascriptAngular cli generate a service and include the provider in one stepStack Overflow
版权声明:本文标题:javascript - Angular cli generate a service and include the provider in one step - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736844873a1955258.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论