Bird
0
0

You have two unrelated Angular components that need to share user login status. Which approach is best to communicate this data?

hard📝 Application Q15 of 15
Angular - Component Interaction
You have two unrelated Angular components that need to share user login status. Which approach is best to communicate this data?
AUse a shared service with an observable to hold and broadcast the login status.
BPass data directly using @Input() and @Output() between the two components.
CUse local variables inside each component without communication.
DUse template reference variables to share data between components.
Step-by-Step Solution
Solution:
  1. Step 1: Identify component relationship

    Unrelated components do not have parent-child relation, so @Input/@Output won't work.
  2. Step 2: Choose communication method

    A shared service with an observable allows broadcasting data to any component.
  3. Final Answer:

    Use a shared service with an observable to hold and broadcast the login status. -> Option A
  4. Quick Check:

    Unrelated components communicate via shared service [OK]
Quick Trick: Use shared service for unrelated component communication [OK]
Common Mistakes:
  • Trying to use @Input/@Output between unrelated components
  • Using local variables that don't share data
  • Misusing template reference variables for communication

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes