Angular service 相当于 SAP Commerce Cloud 里的 service facade.

使用如下的命令行创建Angular service:

ng generate service hero

默认生成的hero.service.ts

You must make the HeroService available to the dependency injection system before Angular can inject it into the HeroesComponent by registering a provider. A provider is something that can create or deliver a service; in this case, it instantiates the HeroService class to provide the service.

Provider负责实例化 service.

看这段TypeScript代码:

@Injectable({providedIn: 'root',
})

When you provide the service at the root level, Angular creates a single, shared instance of HeroService and injects into any class that asks for it. Registering the provider in the @Injectable metadata also allows Angular to optimize an app by removing the service if it turns out not to be used after all.

一个最佳实践:

While you could call getHeroes() in the constructor, that’s not the best practice.
Reserve the constructor for simple initialization such as wiring constructor parameters to properties. The constructor shouldn’t do anything. It certainly shouldn’t call a function that makes HTTP requests to a remote server as a real data service would.
Instead, call getHeroes() inside the ngOnInit lifecycle hook and let Angular call ngOnInit() at an appropriate time after constructing a HeroesComponent instance.

尽量不要在构造函数里编写任何应用逻辑,而是把这些逻辑放到生命周期钩子 ngOnInit里。

在需要使用service 的Component里,首先import service的实现:

使用构造函数参数的方式进行依赖注入:

The parameter simultaneously defines a private heroService property and identifies it as a HeroService injection site.

构造函数参数注入之后,自动生成一个私有的属性,名为heroService,就可以使用该服务了。

运行时效果:

从运行时可以看到,Component构造函数里通过参数完成依赖注入,可以通过this.heroService直接访问注入的服务。

在service的构造函数里设置一个断点,就能观察到Angular框架是在何时实例化这个服务实例的:


要获取更多Jerry的原创文章,请关注公众号"汪子熙":

Angular Service依赖注入的一个具体例子相关推荐

  1. Angular 4 依赖注入教程之一 依赖注入简介

    目录 Angular 4 依赖注入教程之一 依赖注入简介 Angular 4 依赖注入教程之二 组件服务注入 Angular 4 依赖注入教程之三 ClassProvider的使用 Angular 4 ...

  2. Angular依赖注入的一个例子和注入原理单步调试

    定义一个抽象服务类: export abstract class GreetingService {abstract greet(name: string): string;} 定义一个具体类实现该抽 ...

  3. Angular 通过依赖注入机制注入一个对象的例子,什么是 ElementInjector

    假设我在app.config.ts里定义了一个interface AppConfig和一个对象HERO_DI_CONFIG, 我想将后者注入到一个类的构造函数里去: export interface ...

  4. 依赖注入:一个Mini版的依赖注入框架

    前面的章节中,我们从纯理论的角度对依赖注入进行了深入论述,我们接下来会对.NET Core依赖注入框架进行单独介绍.为了让读者朋友能够更好地理解.NET Core依赖注入框架的设计与实现,我们按照类似 ...

  5. Angular 依赖注入的一个常见错误 NullInjectorError, No provider for XXX

    测试代码: export abstract class GreetingService {abstract greet(name: string): string;} 加了@Injectable注解的 ...

  6. .Net Core DI依赖注入:一个接口注入多个实现类

    方式一.默认就可以注入多个实现类 例如接口IShow,注入ShowA和ShowB两个实现类: services.AddTransient<IShow, ShowA>(); services ...

  7. Angular 中的依赖注入link

    Angular 中的依赖注入link 依赖注入(DI)是一种重要的应用设计模式. Angular 有自己的 DI 框架,在设计应用时常会用到它,以提升它们的开发效率和模块化程度. 依赖,是当类需要执行 ...

  8. 【Angular 4】依赖注入

    Angular算是将后端开发工程化引入前端的先驱之一,而Dependency injection依赖注入(后面简称为DI)又是Angular内部运作的核心功能,所以要深入理解Angular有必要先理解 ...

  9. Angular 依赖注入

    问题描述 初学Angular,可能对一堆注解有些懵. 我们一起通过实例来探讨Angular的依赖注入. 一路尝试 @Injectable 一个命令建的StockService,一个手动建的TestSe ...

最新文章

  1. android retrofit 2.0,android – Retrofit 2.0 OnFailure – 原始响应
  2. oracle打开scott用户_Oracle 11gR2中启动Scott用户的方法(推荐)
  3. 78. 子集022(回溯法)
  4. qt 5.0中HeaderView的setResiziMode无法使用的问题
  5. Lemmy仿Reddit社区 v0.8.10源码
  6. java 生成枚举_为什么编译器在Java中生成Enums?
  7. 操作系统中的page cache机制
  8. java中hello类,Java入门篇-hello world
  9. python 爬取历史天气
  10. macOS下R语言入门操作教程
  11. 编写谷歌浏览器插件入门
  12. python词云图实例
  13. Flink流计算编程--watermark(水位线)简介
  14. php getdigest,http digest
  15. 读 Paxos 到 ZooKeeper ¥ 50大洋
  16. 基于 NIOSII 软核的流水灯实验
  17. mysql数据库下订单功能项目_订单功能模块设计与实现
  18. high charts柱状图更新(将原有柱状图更换为其他柱状图)删除重新绘制,柱状图设置渐变色
  19. 实时折线图php mysql 源码_超级漂亮网址导航源码,自助链源码(PHP+MYSQL完整版)...
  20. Computational Intelligence Assisted Design -- In Industrial Revolution 4.0 (计算智能在工业4.0的应用) --绪论

热门文章

  1. E-SKILL网络工程师考试认证必备
  2. [C++ Mind Map] class and memory
  3. 9.使用原生js实现类似于jquery的动画
  4. form表单回车提交问题
  5. jQuery学习笔记:Ajax(二)
  6. ASP+MSSQL注入工具 web版 beta 3 final release
  7. 我是如何学习写一个操作系统(五):故事的高潮之进程和线程1
  8. 从零写一个编译器(七):语义分析之符号表的数据结构
  9. 多线程编程学习笔记——任务并行库(三)
  10. Java命令学习系列