Complete the code to enable tree shaking in an Angular standalone component.
import { Component } from '@angular/core'; @Component({ selector: 'app-sample', template: `<p>Hello Angular!</p>`, standalone: [1] }) export class SampleComponent {}
Setting standalone: true enables Angular to treat the component as standalone, which helps with tree shaking by avoiding unnecessary module imports.
Complete the code to import a module only if it is used, aiding dead code removal.
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; @NgModule({ imports: [ [1] ] }) export class AppModule {}
Only importing BrowserModule when needed helps Angular remove unused modules during build.
Fix the error in the Angular service to allow dead code removal by marking it as injectable only when used.
import { Injectable } from '@angular/core'; @Injectable({ providedIn: [1] }) export class DataService { fetchData() { return 'data'; } }
Using providedIn: 'root' makes the service tree-shakable by Angular, removing it if unused.
Fill both blanks to create a lazy-loaded Angular module that helps with tree shaking and dead code removal.
import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; const routes: Routes = [ { path: 'feature', loadChildren: () => import('./feature/feature.module').[1](m => m.[2]) } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule {}
Using then to access the default export enables lazy loading of the module, which helps Angular remove unused code.
Fill all three blanks to create a conditional import in Angular that supports tree shaking and dead code removal.
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: `<ng-container *ngIf="showFeature"> <ng-container *ngComponentOutlet="([1] | async)"></ng-container> </ng-container>` }) export class AppComponent { showFeature = true; featureComponent = import('./feature/feature.component').[2](m => m.[3]); }
Using import() with then() to get the default export and binding it to featureComponent allows Angular to load the component only when needed, enabling tree shaking.