1

How to define optional parameters in routing of angular2.my routing configuration like this:

<a [routerLink]="['../abc',{xyz: blabla}]">
     and 
<a [routerLink]="['../abc']">

{ path: '/abc/:xyz', component: abc, name: 'abc' },  // Here i want xyz as optional perameter

so the problem is whenever i am using first method with parameter blabla it works fine because at the time of routing i have defined parameter xyz but in case of second method i dont want to send parameter so the URL becomes

http://localhost:8080/#/sideNav/abc/

which is laoding first time fine but after refresh page nothing shows whole window is getting blank with no contents. so is there any method to provide optional parameters while routing in angular2.

i have also tried without defining parameters like this

{ path: '/abc', component: abc, name: 'abc' }

but this will throw error in case of value of xyz is 1 it converts 1 into true

10
  • have you tried both together? Commented Mar 19, 2016 at 6:42
  • yups but not working. Commented Mar 19, 2016 at 7:04
  • why have you used ... syntax before /abc? Commented Mar 19, 2016 at 7:40
  • 1
    I have tested without ... syntax and everything works as expected. Commented Mar 19, 2016 at 7:45
  • 1
    its .. not ... this is because i have used child routing not from root as per need Commented Mar 19, 2016 at 7:50

2 Answers 2

6

You can define multiple routes with and without parameter having the same component:

@RouteConfig([{
  path: '/abc',
  component: Abc,
  name: 'abc'},
{
  path: '/abc/:xyz',
  component: Abc,
  name: 'abcXyz'
}])

and then use the one that you need

<a [routerLink]="['/abcXyz',{xyz: blabla}]">
     and 
<a [routerLink]="['/abc']">

If routeCongig is located in your root file, use / before root's name and if it's on the second level or something, use

<a [routerLink]="['/parentRoot', {parentParams:value}, '/abc']">
Sign up to request clarification or add additional context in comments.

8 Comments

already tried this not working
by the way as of now as has been changed with name
thanks for reminding , updated !!
But why ? what's the error ?
error as explained in question. on refresh does't show anything on window
|
0

You can put both together,

@RouteConfig([
    { path: '/abc/:xyz', component: Abc,  name: 'abc' }
    { path: '/abc', component: Abc, name: 'abc' },
])

if this doesn't work as expected, you should see below,

Optional routeparams #3525
https://github.com/angular/angular/issues/3525

6 Comments

already tried this not working
by the way as of now as has been changed with name
I think now you should look into the issue given to you.
yeah again already saw this issue :p for previous comment see this update stackoverflow.com/a/33196880/5043867
Yes my mistake changing as to name.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.