[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 用来定义一个可以被 ...
随机推荐
- Oracle基础 06 控制文件 controlfile
--查看控制文件路径 show parameter control_files; --控制文件的备份,三种方式1)使用OS命令进行拷贝:1)open状态下,使用alter database命令生成控制 ...
- ANSI、ASCII、Unicode和UTF-8编码
来自:http://blog.163.com/yang_jianli/blog/static/161990006201371451851274/ --------------------------- ...
- 《Java编程思想》笔记 第九章 接口
1.抽象类和抽象方法 抽象方法,仅有方法声明没有方法体 abstract class AbstractClass{ abstract void f(); //没有 {} } 只要有一个或者多个抽象方法 ...
- 对数据访问层的重构(及重构中Perl的应用)
以前上学的时候,听到“一个学生在毕业后刚刚开始编程的头几年中,写出的代码多半是垃圾”这样的说法,均不屑一顾.现在工作一年多了,越发感觉自己代码中疏漏处甚多,故近来常做亡羊补牢的重构之举.拿自己4个月前 ...
- [libgdx游戏开发教程]使用Libgdx进行游戏开发(3)-给游戏添加一些控制功能
每个游戏中都有一些只有程序员自己才知道的控制功能,比如增加金钱,满血复活,无视防御,不死等等. 都是为了方便自己调试而在测试阶段使用的功能. 正如上一章提到的:我们也需要加些只有程序员才知道的控制功能 ...
- 洛谷——P1375 小猫
P1375 小猫 题目描述 有2n只小猫站成一圈,主人小明想把它们两两之间用绳子绑住尾巴连在一起.同时小明是个完美主义者,不容许看到有两根绳子交叉.请问小明有几种连线方案,可以把让所有小猫两两配对? ...
- 索引(Index)
无索引的表就是一个无序的行集.比如下面的人员表中有一些数据: 这个表上没有索引,因此如果我们查找年龄等于17的人员时,必须查看表中的每一行,看它是否与所需的值匹配.这是一个全表扫描,很慢,如果表中只有 ...
- [BZOJ5006][LOJ#2290][THUWC2017]随机二分图(概率+状压DP)
https://loj.ac/problem/2290 题解:https://blog.csdn.net/Vectorxj/article/details/78905660 不是很好理解,对于边(x1 ...
- [BZOJ 2208] 连通数
Link: BZOJ 2208 连通数 Solution: 传递闭包模板题 传递闭包是集合中最小的二元关系,其实就是对二元关系的不断拓展,一般用$floyd$求解 这里要先跑一遍$tarjan$求出$ ...
- [BZOJ 2743] 采花
Link:https://www.lydsy.com/JudgeOnline/problem.php?id=2743 Algorithm: 此题询问区间内出现次数超过1个的数字 明显在线做无从下手,无 ...