[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( ...
随机推荐
- Linux wildcard
Linux中的通配符: 需要注意的是正则表达式与通配符完全是两个东西.wildcard代表的是bash操作接口的一个功能,而正则表达式是一种字符串处理的方法. 例如,'?',在通配符中表示一个字符,在 ...
- Spring Boot 自动配置原理(精髓)
一.自动配置原理(掌握) SpringBoot启动项目会加载主配置类@SpringBootApplication,开启@EnableAutoConfiguration自动配置功能 @EnableAut ...
- MSMQ 跨服务器读写队列的“消息队列系统的访问被拒绝”的解决方案
转自https://www.cnblogs.com/jyz/articles/4612333.html 最近项目中需要跨服务器对消息队列进行读写,开始在单独开发机器上进行Queue的读写没问题.但是部 ...
- mac安装gcc
xcode-select --install安装命令行工具 安装完毕后,可能报错 xcrun: error: active developer path ("/Applications/Xc ...
- javascript常用的操作
1.concat() 连接两个或更多的数组,并返回一个新的数组.注意:该方法不会改变原数组 var arry1=["李四",“王二”]: var arry2=['赵柳',“李旺 ...
- repo 获取各个库的tag代码或者分支代码
关于mainfest.xml中的参数格式和说明,可以自己查阅,此处不详细写,我们知道project中的reversion可以指定分支,tag,commitid等,那么如何书写呢? 首先克隆mainfe ...
- 如何相互转换逗号分隔的字符串和List【转】
将逗号分隔的字符串转换为List 方法 1: 利用JDK的Arrays类 String str = "a,b,c"; List<String> result = Arr ...
- osx brew mysql
MariaDB Server is available for installation on macOS (formerly Mac OS X) via the Homebrew package m ...
- windows程序设计 加载位图图片
现在网上随便下个jpg图片,用windows自带的画图工具打开,点击画图工具左上角,文件->另存为->选择bmp,点击保存,保存好后,就得到一张位图了. 得到的位图,位图的内存比原图片jp ...
- netty如何实现零拷贝
根据 Wiki 对 Zero-copy 的定义: "Zero-copy" describes computer operations in which the CPU does n ...