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. liunx系统问题总结

    1.Unable to locate package错误      解决办法 :输入命令 sudo apt-get update,进行软件的更新

  2. Golang覆盖写入文件的小坑

    记录一点Golang文件操作的笔记,环境:Ubuntu // 删除文件 func removeFile() { err := os.Remove("test.txt") if er ...

  3. Java学习-050-AES256 之 java.security.InvalidKeyException: Illegal key size or default parameters 解决方法

    在进行 Java AES 加密测试时,出现如下错误信息: java.security.InvalidKeyException: Illegal key size or default paramete ...

  4. Docker入门3------手动编辑自定义镜像

    手动编辑自定义镜像 查看本地现有镜像: 基于centos创建一个,会自动下载centos最新原始镜像 docker run -it --name=web centos /bin/bash 然后在容器内 ...

  5. python框架之Django(7)-Cookie&Session使用

    Cookie 添加 response.set_cookie 添加明文cookie response.set_cookie(key, value='', max_age=None, expires=No ...

  6. openshift 容器云从入门到崩溃之八《日志聚合》

    日志可以分为两部分 业务日志 业务日志一般是要长期保留的,以供以后有问题随时查询,elk是现在比较流行的日志方案,但是容器日志最好不要落地所以不能把logstash客户端包在容器里面 可以使用logs ...

  7. 大堆文字不如几张图片-论信息传递的方式以NodeMCU入门为例

  8. C# 使用lambda表达式过滤掉数组中的空字符串

    使用lambda表达式过滤掉数组中的空字符串    KeyWord = KeyWord.Where(S => !string.IsNullOrEmpty(S)).ToArray();

  9. python报错之OSError

    刚刚练一下xlrd模块的时候,竟然报错了! 百度了一下,才知道,是自己直接复制粘贴导致的:最好直接搜索\u202a才找到解决办法 所以只需从写换个地方复制路径或则重新手动输入一次就解决了 参考链接:h ...

  10. [转载]SMTP的几个端口的比较

    出处:https://blog.csdn.net/zhangyuan12805/article/details/78781330 1. SMTP Port 25: 25口是四个端口中最老的.这是在33 ...