Bird
0
0

How should you configure CoreModule to ensure AuthService is a singleton across the app and avoid multiple instances?

hard📝 Application Q9 of 15
Angular - Modules
Consider two Angular modules: CoreModule and FeatureModule. CoreModule provides a singleton service AuthService. How should you configure CoreModule to ensure AuthService is a singleton across the app and avoid multiple instances?
AProvide <code>AuthService</code> in both <code>CoreModule</code> and <code>FeatureModule</code>
BProvide <code>AuthService</code> in <code>FeatureModule</code> only
CProvide <code>AuthService</code> only in <code>CoreModule</code> and import <code>CoreModule</code> once in <code>AppModule</code>
DAdd <code>AuthService</code> to <code>declarations</code> in <code>CoreModule</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand singleton service pattern

    To have a singleton service, provide it once in a module imported only once, typically CoreModule.
  2. Step 2: Avoid multiple providers

    Providing the service in multiple modules creates multiple instances, breaking singleton behavior.
  3. Final Answer:

    Provide AuthService only in CoreModule and import CoreModule once in AppModule -> Option C
  4. Quick Check:

    Singleton service = single provider in single imported module [OK]
Quick Trick: Provide singleton services once in CoreModule imported once [OK]
Common Mistakes:
  • Providing service in multiple modules
  • Adding services to declarations
  • Importing CoreModule multiple times

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes