Bird
0
0

How can you combine a BehaviorSubject with Angular's inject() function to create a simple store in a standalone component?

hard📝 Application Q9 of 15
Angular - State Management
How can you combine a BehaviorSubject with Angular's inject() function to create a simple store in a standalone component?
AUse <code>inject()</code> only for Angular modules, not stores
BCreate a new BehaviorSubject inside the component without injection
CInject the store service using <code>inject()</code> and subscribe to its BehaviorSubject
DUse <code>inject()</code> to create a BehaviorSubject instance directly
Step-by-Step Solution
Solution:
  1. Step 1: Understand inject() usage

    Angular's inject() is used to get instances from dependency injection.
  2. Step 2: Apply to store pattern

    Inject the store service that holds the BehaviorSubject and subscribe to it in the component.
  3. Final Answer:

    Inject the store service using inject() and subscribe to its BehaviorSubject -> Option C
  4. Quick Check:

    Use inject() to get store service, not create BehaviorSubject directly = D [OK]
Quick Trick: Use inject() to get store service, then subscribe BehaviorSubject [OK]
Common Mistakes:
  • Creating BehaviorSubject inside component without injection
  • Trying to create BehaviorSubject with inject()
  • Misunderstanding inject() purpose

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes