[Angular] 'providedIn' for service
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的更多相关文章
- Angular:使用service进行http请求的简单封装
①使用ng g service services/storage创建一个服务组件 ②在app.module.ts 中引入HttpClientModule模块 ③在app.module.ts 中引入创建 ...
- [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 ...
- angular js自定义service的简单示例
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Angular 学习笔记——service &constant
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- [Angular 2] Inject Service
TypeScript is used heavily as we build up our application, but TypeScript isn’t required. If you wan ...
- [Angular 2] Use Service use Typescript
When creating a service, need to inject the sercive into the bootstrap(): import {bootstrap, Compone ...
- Angular:使用service进行数据的持久化设置
①使用ng g service services/storage创建一个服务组件 ②在app.module.ts 中引入创建的服务 ③利用本地存储实现数据持久化 ④在组件中使用
- Angular Service入门
1.Angular内置service Angular为了方便开发者开发,本身提供了非常多的内置服务.可以通过https://docs.angularjs.org/api/ng/service查看Ang ...
- Angular service, 服务
早上开车上班, 发现车快没油了, 于是拐进加油站. 有一辆出租车也在加油.. Angular service在一个应用里是以单例形式存在的. 这个单例的实例是由service factory( ...
随机推荐
- day12 十二、开放封闭、装饰器
一.nonlocal关键词 # global # num = # def fn(): # global num # L>G 将局部的名字与全局统一 # num = # fn() # print( ...
- 前端模板 artTemplate之辅助方法template.helper
var labelMap = { onlinePayment:{ label:"在线支付", desc:"支持大部分储蓄卡.信用卡及第三方平台支付", name ...
- 20165317 java学习总结
20165317 java学习总结 每周作业链接汇总 预备作业1:https://www.cnblogs.com/ningxinyu/p/8341213.html 预备作业2:https://www. ...
- spark-sql集合的“条件过滤”,“合并”,“动态类型映射DataFrame”,“存储”
List<String> basicList = new ArrayList<String>(); basicList.add("{\"name\" ...
- Windows10 家庭版 关闭Windows defender
管理员权限打开cmd,输入下面命令: reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender&quo ...
- 台式电脑、笔记本快捷选择启动项Boot 快捷键大全
我们在安装系统时,会去设置电脑是从硬盘启动.U盘启动.光驱启动.网卡启动. 一般设置的方法有两种:一种是进BIOS主板菜单设置启动项顺序:另一种就是我在这里要介绍的快捷选择启动项. 以下是网友整理的各 ...
- IE 浏览器background image 属性问题
background-size 如果以百分比的形式设置参数,第一个参数是宽度,第二页参数是高度.“如果只设置第一个参数,则第二个参数为auto”.这样设置,在Firefox chrome 浏览器中,图 ...
- [转][访谈] Olivier Grisel谈scikit-learn和机器学习技术的未来
原文:http://www.csdn.net/article/2015-10-11/2825882 几周前,我们的Florian Douetteau (FD)对Olivier Grisel(OG)进行 ...
- sitecore开发入门教程如何获取Sitecore项目的域名
我假设您在<sites>web.config文件的部分中设置了多个站点,并且每个站点都hostName定义了一个属性,例如 <site name="website1&quo ...
- python locust 性能测试:locust参数-保证并发测试数据唯一性,循环取数据
from locust import TaskSet, task, HttpLocustimport queue class UserBehavior(TaskSet): @task def test ...