There is now a new, recommended, way to register a provider, directly inside the @Injectable() decorator, using the new providedIn attribute.

@Injectable({
providedIn: 'root'
})
export class UserService { }

When you use 'root', your injectable will be registered as a singleton in the application, and you don’t need to add it to the providers of the root module. Similarly, if you use providedIn: UsersModule, the injectable is registered as a provider of the UsersModule without adding it to the providers of the module.

In the same spirit, you can now declare an InjectionToken and directly register it with providedIn and give it a factory:

 export const baseUrl = new InjectionToken<string>('baseUrl', {
providedIn: 'root',
factory: () => 'http://localhost:8080/'
});

Note that it also simplifies unit testing. We used to register the service in the providers of the testing module to be able to test it.

Before:

beforeEach(() => TestBed.configureTestingModule({
providers: [UserService]
}));

Now, if the UserService uses providedIn: 'root':

After:

beforeEach(() => TestBed.configureTestingModule({}));

[Angular] 'providedIn' for service的更多相关文章

  1. Angular:使用service进行http请求的简单封装

    ①使用ng g service services/storage创建一个服务组件 ②在app.module.ts 中引入HttpClientModule模块 ③在app.module.ts 中引入创建 ...

  2. [Angular 2] Inject Service with "Providers"

    In this lesson, we’re going to take a look at how add a class to the providers property of a compone ...

  3. angular js自定义service的简单示例

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. Angular 学习笔记——service &constant

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  5. [Angular 2] Inject Service

    TypeScript is used heavily as we build up our application, but TypeScript isn’t required. If you wan ...

  6. [Angular 2] Use Service use Typescript

    When creating a service, need to inject the sercive into the bootstrap(): import {bootstrap, Compone ...

  7. Angular:使用service进行数据的持久化设置

    ①使用ng g service services/storage创建一个服务组件 ②在app.module.ts 中引入创建的服务 ③利用本地存储实现数据持久化 ④在组件中使用

  8. Angular Service入门

    1.Angular内置service Angular为了方便开发者开发,本身提供了非常多的内置服务.可以通过https://docs.angularjs.org/api/ng/service查看Ang ...

  9. Angular service, 服务

      早上开车上班, 发现车快没油了, 于是拐进加油站. 有一辆出租车也在加油..   Angular service在一个应用里是以单例形式存在的. 这个单例的实例是由service factory( ...

随机推荐

  1. React中redux表单编辑

    reduxForm中反写数据在输入框中,数据是从别的模块拉取 // 编辑应用表单 class EditCode extends React.Component { constructor(props) ...

  2. [java] 在linux+chrome/firefox上使用java applet

    我现在的浏览器用不了java applet,我的环境是archlinux + chrome or firefox. java网站的指引是这样说的 按照这些说明操作,以便在 Ubuntu Linux 上 ...

  3. JDBC事务(一)

    package cn.sasa.tran01; import java.sql.Connection; import java.sql.DriverManager; import java.sql.P ...

  4. qemu对虚拟机的内存管理(二)

    上篇文章主要分析了qemu中对虚拟机内存管理的关键数据结构及他们之间的联系,这篇文章则主要分析在地址空间发生变化时,如何将其更新至KVM中,保持用户空间与内核空间的同步. 这一系列操作与之前说的Add ...

  5. Date Calendar

    1 毫秒值概念 时间和日期的计算,必须依赖毫秒值获取当前日期的毫秒值:System.currentTimeMillis() 返回值long类型参数, 时间原点:公元1970年1月1日,午夜0:00:0 ...

  6. C#基础加强(2)之密闭类、静态类及扩展方法

    密闭类 简介 密闭类是被 sealed 关键字修饰的类,密闭类不能有子类.一般只有系统的一些基本类声明为密闭类,例如 String 类. 相关面试题 是否可以编写一个类继承自 String 类? 我们 ...

  7. 【JVM】-NO.110.JVM.1 -【JDK11 HashMap详解】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  8. Linux下安装whl文件

    直接使用pip安装: [root@mycentos ~]# pip install *.whl

  9. ionic3 在ios9.0 系统下 会出现ReferenceError:Can't find variable:Intl 错误提示

    ionic3 框架开发app  在ios 9.0版本中 ReferenceError:Can't find variable:Intl 错误提示: 在index.html 文件中添加 <scri ...

  10. Python3.0以上版本在对比图片相似中的应用

    首先声明一下,代码是从网上找到的,只是本人作以简单的修改. 请大家尊重原创. 我本地用到的是 Python 3.4   以及 Pillow (4.0.0)  第三方包. 方法一. #!/usr/bin ...