Bird
0
0

You want to extract two route parameters, 'category' and 'id', and react to changes in both. Which code snippet correctly achieves this?

hard📝 component behavior Q8 of 15
Angular - Routing
You want to extract two route parameters, 'category' and 'id', and react to changes in both. Which code snippet correctly achieves this?
Athis.route.snapshot.params['category']; this.route.snapshot.params['id'];
Bthis.route.queryParams.subscribe(params => { this.category = params['category']; this.id = params['id']; });
Cthis.route.params.subscribe(params => { this.category = params['category']; this.id = params['id']; });
Dthis.route.params['category']; this.route.params['id'];
Step-by-Step Solution
Solution:
  1. Step 1: Use params observable for dynamic parameters

    To react to changes, subscribe to params observable and extract both parameters inside the callback.
  2. Step 2: Evaluate options

    this.route.params.subscribe(params => { this.category = params['category']; this.id = params['id']; }); subscribes correctly and assigns both parameters. this.route.snapshot.params['category']; this.route.snapshot.params['id']; uses snapshot which is static. this.route.queryParams.subscribe(params => { this.category = params['category']; this.id = params['id']; }); uses queryParams which is for query string, not route params. this.route.params['category']; this.route.params['id']; tries to access params as an object without subscription.
  3. Final Answer:

    this.route.params.subscribe(params => { this.category = params['category']; this.id = params['id']; }); -> Option C
  4. Quick Check:

    Subscribe to params for multiple route params = B [OK]
Quick Trick: Subscribe to params to get multiple dynamic route parameters [OK]
Common Mistakes:
MISTAKES
  • Using snapshot for dynamic param changes
  • Confusing queryParams with params
  • Accessing params without subscription

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes