Tree shaking vs. Non tree shaking providers in Angular

In our example we are importing and referencing our Service in the AppModule causing an explicit dependency that cannot be tree shaken.

import { NgModule } from '@angular/core';

import { BrowserModule } from '@angular/platform-browser';

 

import { AppComponent } from './app.component';

import { SharedService } from './shared.service';

 

@NgModule({

  imports: [BrowserModule, FormsModule],

  declarations: [AppComponent],

  bootstrap: [AppComponent],

  providers: [SharedService]

})

export class AppModule {}

By using tree shaking, we can make sure our application only includes the code that is needed for our application to run.

import { Injectable } from '@angular/core';

 

@Injectable({

  providedIn: 'root'

})

export class SharedService {

  constructor() {

    console.log('SharedService instantiated');

  }

}



要查看或添加评论,请登录

Piyali Das的更多文章

  • Accessible Bootstrap Dropdown Navigation

    Accessible Bootstrap Dropdown Navigation

    The Focus Focus refers to what element in your application (such as a field, checkbox, button, or link) currently…

  • Front-end development using Gulp

    Front-end development using Gulp

    Gulp is a JavaScript toolkit which helps you in implementing multiple front end tasks during web development, it can be…

  • Angular Dynamic Context Menu

    Angular Dynamic Context Menu

    "contextmenu" is an event like click, mouseup, mousemove, mousedown. This "contextmenu" event is typically triggered by…

    2 条评论
  • Right-click Context Menu In JavaScript

    Right-click Context Menu In JavaScript

    "contextmenu" is an event like click, mouseup, mousemove, mousedown. This "contextmenu" event is typically triggered by…

  • Dynamic Height in Angular Application

    Dynamic Height in Angular Application

    In this tutorial, i have explained how you an dynamically set height of a div of component depending on other div of…

  • Code Optimization Advantage with Example

    Code Optimization Advantage with Example

    Dynamic Font Change non-optimize code HTML code…

  • Advantages of Angular Library in Architectural Design

    Advantages of Angular Library in Architectural Design

    Application Strategic Design Decompose the big application into smaller libraries Smaller libraries are easily…

  • Angular Multi Application Project Creation

    Angular Multi Application Project Creation

    If your installed Angular global version is different and you want to create different version Angular application…

    1 条评论
  • Angular Port Change

    Angular Port Change

    3 steps to change port in Angular Change in Angular.json Change in script of package.

    2 条评论

社区洞察

其他会员也浏览了