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. css学习_css浮动

    1.文档流介绍 网页布局的核心就是利用css来摆放盒子, 把盒子摆放在合适的位置. css的定位机制有以下3种(网页布局一般需要3种搭配使用): a.普通流(标准流) b.浮动 1.浮动只有左右. 2 ...

  2. Luogu 1071 - 潜伏者 - [字符串]

    题目链接:https://www.luogu.org/problemnew/show/P1071 题解: 模拟就完事儿了. 注意failed的情况有:出现一个 $f[x]$ 对应多个值:存在两个不同的 ...

  3. mongodb 3.2 分片 + 副本集

    从图中可以看到有四个组件:mongos.config server.shard.replica set. mongos,数据库集群请求的入口,所有的请求都通过mongos进行协调,不需要在应用程序添加 ...

  4. 流控制、FlowControl

    这个选项基本上所有网卡都会有,但是叫法会有些差别,比如Realtek网卡叫做流控制,Intel网卡叫做流程控制,还有一些网卡选项干脆是英文的,叫做FlowControl,很多交换机上也有这个功能,也叫 ...

  5. 2018-2019-1 20189203《Linux内核原理与分析》第八周作业

    第一部分 课本学习 ELF文件(目标文件)格式主要三种: 1)可重定向文件:文件保存着代码和适当的数据,用来和其他的目标文件一起来创建一个可执行文件或者是一个共享目标文件.(目标文件或者静态库文件,即 ...

  6. Hibernate查询操作

    操作前需要创建好Hibernate项目,创建项目,可参考:http://www.cnblogs.com/zhaojinyan/p/9336174.html 一下的例子是从其他贴子粘过来的(知识无国界! ...

  7. HTML常用标签定义,用法及例子

    1.HTML注释 <!--...--> 定义:使用注释可对代码进行解释,不会显示在浏览器中. <!--这是一段呢注释.注释不会在浏览器中显示.--> <p>段落标签 ...

  8. 关于ajax返回数据处理

    查看jquery文档,我们知道jquery有很多种Ajax调用方法,下面结合springmvc返回的数据,假设返回的是data ='{"label":"1",& ...

  9. DRF之解析器源码解析

    解析器 RESTful一种API的命名风格,主要因为前后端分离开发出现前后端分离: 用户访问静态文件的服务器,数据全部由ajax请求给到 解析器的作用就是服务端接收客户端传过来的数据,把数据解析成自己 ...

  10. GO语言常量和变量

    标识符与关键字 标识符 人为定义有特殊意义的词,Go语言中标识符由字母数字和_(下划线)组成,并且只能以字母和_开头. 关键字 关键字是指编程语言中预先定义好的具有特殊含义的标识符. GO语言中有25 ...