[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( ...
随机推荐
- 洛谷P3376 【模板】网络最大流
题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...
- .NET Core 的 Span<T> 学习与使用笔记
一.阅读材料 All About Span: Exploring a New .NET Mainstay Span<T> - byte to int conversions Span< ...
- 关于java中Pattern和Matcher区别于联系
本文章转自: http://blog.csdn.net/cclovett/article/details/12448843 结论:Pattern与Matcher一起合作.Matcher类提供了对正则表 ...
- arcpy加载mxd文件时,无效的MXD路径,提示assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
无效的MXD路径,将路径前加‘u’,改为这种: mxdPath = u"C:\\1331\\DB\\Original Files\\dd.mxd" 参考: https://gis. ...
- c++代码检测工具
cppcheck是一款静态代码检查工具,可以检查如内存泄漏等代码错误,使用起来比较简单,即提供GUI界面操作,也可以与VS开发工具结合使用. 1.安装 一般会提供免安装版,安装完成后将cppcheck ...
- SQL UPDATE嵌套使用
遇见的问题 1.更新语句,更新字段 UPDATE number SET sumab = 2 WHERE id =1 求和 SELECT SUM(num_a+num_b)FROM number WHER ...
- 20165330 2017-2018-2 《Java程序设计》第7周学习总结
课本知识总结 第十一章 JDBC与MySQL数据库 安装XAMPP软件及启动MySQL 下载链接:XAMPP 安装步骤:参考教程xampp新手学习指引(windows示例) 启动MySQL:打开系统c ...
- PyQt5简介及demo
PyQt5说明 pyqt5是一套Python绑定Digia QT5应用的框架.它可用于Python 2和3.本教程使用Python 3.Qt库是最强大的GUI库之一.pyqt5的官方网站http:// ...
- 解决Windows下文件在Linux下打开出现乱码的问题
目录 问题 原理 解决 总结 参考资料 问题 前几天生病了,Java一直在看代码但是没跟着打,于是决定偷一波小小的懒,直接把教材的代码从Windows通过共享文件夹放到了Linux里面.但是编译的时候 ...
- PHP on CentOS (LAMP) and wordpress
http://php.net/manual/zh/install.windows.php https://www.unixmen.com/install-wordpress-centos-7-linu ...