Angular - Services and Dependency InjectionHow can you ensure a service is tree-shakable and only included if used in the app?AProvide the service in a lazy-loaded module only.BUse <code>@Injectable({ providedIn: 'root' })</code> in the service.CDeclare the service without @Injectable decorator.DManually add the service to the providers array in AppModule.Check Answer
Step-by-Step SolutionSolution:Step 1: Understand tree-shakable providersUsing providedIn: 'root' enables Angular to tree-shake unused services.Step 2: Compare with manual providersManually adding to providers array prevents tree-shaking because Angular always includes it.Final Answer:Use @Injectable({ providedIn: 'root' }) in the service. -> Option BQuick Check:providedIn 'root' enables tree-shaking [OK]Quick Trick: Use providedIn 'root' for tree-shakable services [OK]Common Mistakes:MISTAKESAdding service manually disables tree-shakingOmitting @Injectable disables injectionAssuming lazy module alone ensures tree-shaking
Master "Services and Dependency Injection" in Angular9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More Angular Quizzes HTTP Client - GET requests - Quiz 4medium HTTP Client - GET requests - Quiz 13medium Routing - Router outlet for view rendering - Quiz 4medium Routing - Lazy loading modules with routes - Quiz 3easy RxJS Operators - mergeMap vs concatMap vs exhaustMap - Quiz 10hard Services and Dependency Injection - How dependency injection works in Angular - Quiz 13medium Services and Dependency Injection - Creating a service with CLI - Quiz 13medium Template-Driven Forms - ngModel for form binding - Quiz 14medium Template-Driven Forms - Showing validation errors - Quiz 11easy Template-Driven Forms - ngForm directive and form state - Quiz 5medium