Bird
0
0

Consider this Angular component snippet:

medium📝 component behavior Q4 of 15
Angular - Routing
Consider this Angular component snippet:
constructor(private route: ActivatedRoute) { }

ngOnInit() {
  this.route.params.subscribe(params => {
    console.log(params['productId']);
  });
}

If the route is configured as path: 'product/:productId' and the URL is /product/99, what will be logged?
A99
Bundefined
CproductId
DAn error will be thrown
Step-by-Step Solution
Solution:
  1. Step 1: Understand route configuration

    The route has a parameter named productId.
  2. Step 2: Check URL parameter

    The URL /product/99 sets productId to '99'.
  3. Step 3: Subscription behavior

    The subscription to params emits an object with productId key and value '99'.
  4. Final Answer:

    99 -> Option A
  5. Quick Check:

    Route param matches URL segment value [OK]
Quick Trick: Route param keys match URL segments exactly [OK]
Common Mistakes:
MISTAKES
  • Expecting undefined if parameter is present
  • Confusing parameter name with its value
  • Assuming subscription throws error on valid params

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes