[Angular] Tree shakable provider
When we create a Service, Angluar CLI will helps us to add:
@#Injectable({
providedIn: 'root'
})
It only create a instance in root dependency tree. If there is no reference to use this provider, Angular will remove it from our production code.
But the service we created are Class based service, what if we want to create some Object and inject this Object to our application and we want to make it tre shakable as well.
We can do as following:
import { InjectionToken } from "@angular/core";
export interface AppConfig {
apiUrl: string;
courseCacheSize: number;
}
export const APP_CONFIG: AppConfig = {
apiUrl: "http://localhost:9000",
courseCacheSize:
};
// Use providedIn & factory to make it as tree shakable provider.
export const CONFIG_TOKEN = new InjectionToken<AppConfig>("CONFIG_TOKEN", {
providedIn: "root",
factory: () => APP_CONFIG
});
// Not tree shakable
// export const CONFIG_TOKEN = new InjectionToken<AppConfig>("CONFIG_TOKEN");
Whereever you use the provider, you need to remove it:
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"],
// Remove it when need to use tree shakable provider
providers: [{ provide: CONFIG_TOKEN, useValue: APP_CONFIG }]
})
[Angular] Tree shakable provider的更多相关文章
- angular factory Services provider 自定义服务 工厂
转载于 作者:海底苍鹰地址:http://blog.51yip.com/jsjquery/1602.html 1.在app.js 中声明了模块的依赖 var phonecatApp = angular ...
- [Angular 2] Factory Provider
In this lesson, we discuss how and when to use factory providers, to enable dependencies that should ...
- Angular 学习笔记——$provider
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- ionic准备之angular基础———服务provider 和 factory和service(9)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 生成树形结构的json字符串代码(c#)供前端angular tree使用.
框架是使用EF6.0.可以针对返回的值使用Newtonsoft.Json.dll(百度搜一下)来对返回的值序列化为json字符串,如果对以下值那就是使用JsonConvert.SerializeObj ...
- [Angular 2] Factory Provider with dependencies
This lesson discusses when and how to add dependencies, resolved by Angular’s DI, to factory provide ...
- [Angular] Using useExisting provider
Unlike 'useClass', 'useExisting' doesn't create a new instance when you register your service inside ...
- angular内置provider之$compileProvider
一.方法概览 directive(name, directiveFactory) component(name, options) aHrefSanitizationWhitelist([regexp ...
- angular service provider
关于 angular service factory provider 方面有很多,我也来写一篇加深下印象 provider 是一切方法的基础,所以功能也最强,provider 用来定义一个可以被 ...
随机推荐
- JS计算两个日期之间的天数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 7.Redis键值对数据库
1.Redis的安装以及客户端连接 安装:apt-get install redis-server 卸载:apt-get purge --auto-remove redis-server 启动:red ...
- Linux下文件的三个时间意义及用法
Linux下文件的三个时间参数: (1)modification time(mtime):内容修改时间 这里的修改时间指的是文件的内容发生变化,而更新的时间. (2)change tim ...
- hdu4240 求一条流量最大的路/(此题网上百分之90以上算法是错误的)
题意:求最大流/一条流量最大的路的流量.(此题HDU上数据水,下面俩种错误的都能过....) 思路1;每次增广的时候更新流量,保存最大的那条. 错误性:每次更新,有可能最大的那条流量是前几次已经增广 ...
- AC日记——病毒侵袭持续中 hdu 3065
3065 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...
- (十一)数组array
变量:只能存一个值,数组可以存多个值 (1)普通数组,索引下标是整数: 1)定义: 方法一:一次赋一个值:语法:数组名[下标]=变量值 array[1]=linux array[2]=shell 方法 ...
- Servlet 调用过程
上图的大概意思: 前台输入访问路径后,浏览器会去访问本地的host文件查询有木有与之匹配域名的IP地址,若无则在访问DNS服务器查询与之匹配的IP地址.解析IP后则开始发起HTTP请求,根据请求中的基 ...
- [php] 解析JSON字符串
$json_str = '{"thumb":"","photo":[{"url":"557a3d2184db0 ...
- ACM竞赛常用头文件模板-备忘
备忘. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...
- 康托展开&逆展开算法笔记
康托展开(有关全排列) 康托展开:已知一个排列,求这个排列在全排列中是第几个 康托展开逆运算:已知在全排列中排第几,求这个排列 定义: X=an(n-1)!+an-1(n-2)!+...+ai(i-1 ...