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 }]
})

最新文章

  1. JavaScript Object对象
  2. JS原型链
  3. Hibernate的三种状态
  4. choco命令
  5. HDU3530 子序列
  6. xcode 开发ios兼容性问题的上下黑边 和 coco2d-x 游戏分辨率适配 ResolutionPolicy::FIXED_WIDTH 都会引起上下黑边问题!!!
  7. [BZOJ 3888] [Usaco2015 Jan] Stampede 【线段树】
  8. 模板--&gt;中国剩余定理[互质版本]
  9. 马士兵 Servlet &amp; JSP(1) Servlet (源代码)
  10. JavaScript 常用小代码
  11. MYSQL 巧用count,sum进行统计数据
  12. HDU - 1116 Play on Words(欧拉图)
  13. S3C2440之IIC裸机驱动
  14. Storm官方文档翻译之设置开发环境
  15. Django 模型和数据库 总结
  16. python 闯关之路三(面向对象与网络编程)
  17. Linux kernel的中断子系统之(七):GIC代码分析
  18. self_vs_default_definee_vs_receiver
  19. IIS 发布ASP.NET MVC 4.0 错误500.21解决办法
  20. python 进程锁 生产者消费者模型 队列 (进程其他方法,守护进程,数据共享,进程隔离验证)

热门文章

  1. linux下卸载Oracle
  2. 聊聊C++模板函数与非模板函数的重载
  3. python unittest 快速入门
  4. docker从零开始(二)容器初体验
  5. hdu 1140(三维)
  6. *****git pull总结
  7. APP中常见上下循环滚动通知的简单实现,点击可进入详情
  8. Delphi Socket通信及多线程编程总结
  9. AC日记——魔方 洛谷 P2007
  10. Django中遇到的mysql问题