Bird
0
0

What is wrong with this code if the goal is to log the route parameter 'id' whenever it changes?

medium📝 Debug Q7 of 15
Angular - Routing
What is wrong with this code if the goal is to log the route parameter 'id' whenever it changes?
ngOnInit() {
  const id = this.route.snapshot.params['id'];
  console.log(id);
}
AShould use queryParams instead of params
Bsnapshot.params is undefined
CIt only logs once and misses future changes
DMissing subscription to params observable
Step-by-Step Solution
Solution:
  1. Step 1: Understand snapshot behavior

    snapshot.params gives the parameters at component creation time only.
  2. Step 2: Identify limitation

    If the route parameter changes while the component is active, snapshot.params does not update, so changes are missed.
  3. Final Answer:

    It only logs once and misses future changes -> Option C
  4. Quick Check:

    snapshot is static, misses param changes = A [OK]
Quick Trick: Use params observable to catch param changes, not snapshot [OK]
Common Mistakes:
MISTAKES
  • Using snapshot for dynamic parameters
  • Confusing params with queryParams
  • Not subscribing to params observable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes