Angular cli生成服务并在一个步骤中包含提供程序 [英] Angular cli generate a service and include the provider in one step

查看:118
本文介绍了Angular cli生成服务并在一个步骤中包含提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用angular cli生成服务,并在 app.module.ts 中将其作为提供商添加到一个步骤中,或者使用特殊选项 ng g service 命令?

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?

执行时:

$ 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

旁边,(并根据警告消息)我通常使用文本编辑器将其添加到 app.module.ts 提供商部分:

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?

推荐答案

实际上,有可能提供服务(或警卫,因为这也需要在创建服务时提供。

Actually, 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 gs services / backendApi --module = app.module

编辑

也可以提供给功能模块,你必须给它一个你想要的模块的路径。

It is possible to provide to a feature module, as well, you must give it the path to the module you would like.

ng gs services / backendApi --module = services / services.module

这篇关于Angular cli生成服务并在一个步骤中包含提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆