Bird
0
0

What is wrong with this RouterLink usage?

medium📝 Debug Q7 of 15
Angular - Routing
What is wrong with this RouterLink usage?
<a [routerLink]="['/product', productId]">Product</a>

Assuming productId is undefined.
AThe link will not render in the DOM.
BThe link will navigate to '/product/undefined', which is likely unintended.
CAngular will throw a runtime error and stop navigation.
DAngular will replace undefined with an empty string automatically.
Step-by-Step Solution
Solution:
  1. Step 1: Understand undefined parameter in RouterLink

    If a parameter is undefined, Angular converts it to the string 'undefined' in the URL.
  2. Step 2: Analyze consequences

    This results in a URL like '/product/undefined', which is usually not desired.
  3. Final Answer:

    The link will navigate to '/product/undefined', which is likely unintended. -> Option B
  4. Quick Check:

    Undefined param becomes 'undefined' string in URL [OK]
Quick Trick: Check variables passed to RouterLink for undefined [OK]
Common Mistakes:
MISTAKES
  • Expecting Angular to error on undefined
  • Ignoring undefined params
  • Assuming empty string replaces undefined

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes