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的更多相关文章

  1. angular factory Services provider 自定义服务 工厂

    转载于 作者:海底苍鹰地址:http://blog.51yip.com/jsjquery/1602.html 1.在app.js 中声明了模块的依赖 var phonecatApp = angular ...

  2. [Angular 2] Factory Provider

    In this lesson, we discuss how and when to use factory providers, to enable dependencies that should ...

  3. Angular 学习笔记——$provider

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  4. ionic准备之angular基础———服务provider 和 factory和service(9)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 生成树形结构的json字符串代码(c#)供前端angular tree使用.

    框架是使用EF6.0.可以针对返回的值使用Newtonsoft.Json.dll(百度搜一下)来对返回的值序列化为json字符串,如果对以下值那就是使用JsonConvert.SerializeObj ...

  6. [Angular 2] Factory Provider with dependencies

    This lesson discusses when and how to add dependencies, resolved by Angular’s DI, to factory provide ...

  7. [Angular] Using useExisting provider

    Unlike 'useClass', 'useExisting' doesn't create a new instance when you register your service inside ...

  8. angular内置provider之$compileProvider

    一.方法概览 directive(name, directiveFactory) component(name, options) aHrefSanitizationWhitelist([regexp ...

  9. angular service provider

    关于  angular service factory  provider 方面有很多,我也来写一篇加深下印象 provider 是一切方法的基础,所以功能也最强,provider 用来定义一个可以被 ...

随机推荐

  1. Spring JdbcTemplate框架搭建及其增删改查使用指南

    Spring JdbcTemplate框架搭建及其增删改查使用指南 前言: 本文指在介绍spring框架中的JdbcTemplate类的使用方法,涉及基本的Spring反转控制的使用方法和JDBC的基 ...

  2. celery-分布式任务队列-原理

    # 转自:https://www.cnblogs.com/forward-wang/p/5970806.html 在学习Celery之前,我先简单的去了解了一下什么是生产者消费者模式. 生产者消费者模 ...

  3. python中的迭代器详解

    #原创,转载请先联系 理论性的东西有点枯燥,耐心点看- 1.迭代是什么? 我们知道可以对list,tuple,dict,str等数据类型使用for...in的循环语法,从其中依次取出数据,这个过程叫做 ...

  4. solr params.json

    The Request Parameters API allows creating parameter sets that can override or take the place of par ...

  5. [BZOJ3027][Ceoi2004]Sweet 容斥+组合数

    3027: [Ceoi2004]Sweet Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 135  Solved: 66[Submit][Status] ...

  6. php+mysql折线图

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  7. FZU 2150 Fire Game 【两点BFS】

    Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...

  8. 【我要学python】愣头青之初安装就打了一记耳光

    pycharm安装好后创建项目出现interpreter field is empty,导致pycharm无法使用. 这是因为python没有安装好,重新自定义安装一次即可 下载地址:https:// ...

  9. HDUOJ题目HTML的爬取

    HDUOJ题目HTML的爬取 封装好的exe/app的GitHub地址:https://github.com/Rhythmicc/HDUHTML 按照系统选择即可. 其实没什么难度,先爬下来一个题目的 ...

  10. leetcode116 Populating Next Right Pointers in Each Node

    题意:给一个完全二叉树: 1 / \ 2 3 / \ / \ 4 5 6 7 让左子树的next指针指向右子树,右子树的next继续指向右边,变成了这样: 1 -> NULL / \ 2 -&g ...