Repository navigation
Description
I'm submitting a...
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
Optional param will not work if
(1) last component hast empty path,
(2) it's parent component has named path.
<a [routerLink]="[{param1: 'test'}]">This link will cause error</a>

Expected behavior
url parameter changed normally.
Minimal reproduction of the problem with instructions
https://angular-gjpvor.stackblitz.io/crisis-center/center
Open console to see errors.
What I have changed to the code from official example:
- change path of
CrisisListComponentfrom''to'center':
const crisisCenterRoutes: Routes = [
{
path: '',
component: CrisisCenterComponent,
children: [
{
path: 'center', <=== parent has named path
component: CrisisListComponent,
children: [
{
path: ':id',
component: CrisisDetailComponent,
canDeactivate: [CanDeactivateGuard],
resolve: {
crisis: CrisisDetailResolver
}
},
{
path: '', <=== Empty path
component: CrisisCenterHomeComponent
}
]
}
]
}
];
- put routerLink in
CrisisCenterHomeComponent
<div style='border: red 1px solid; padding:10px;'>
<a [routerLink]="[{param1: 'test'}]">This link will cause error</a>
</div>
or call router.navigate([{param: 'value'}], {relativeTo: this.route}) in component code will also cause error.
I have debugged and found some possible problem.
See this line
when error happens, startIndex is bigger than segmentGroup.segments.length, so p is undefined, p.path will be invalid.
Seems segmentGroup.segments is from url. For url /path/to/child, this array will contains 3 elements. If we have components with empty path, route levels will be bigger than that. startIndex will also bigger than that. Perhaps there is a bug here.
startIndex equals ActivatedRouteSnapshot._lastIndex of current component.
here is an example from out project:

Route definition of a sub module.
const routes: Routes = [
{
path: '',
component: ProjectSettingsComponent,
children: [
{
path: '',
component: ProjectSettingsHomeComponent
},
{
path: 'project-roles',
component: ProjectRolesComponent
},
{
path: 'workflow',
component: WorkflowWrapperComponent,
children: [
{
path: '', //TODO: if empty, will cause error: ERROR TypeError: Cannot read property 'path' of undefined
component: WorkflowComponent
},
{
path: 'node/:workflowNodeId',
component: WorkflowNodeConfigComponent
}
]
},
{
path: 'fields',
component: ProjectFieldsComponent
}
]
}
];
What is the motivation / use case for changing the behavior?
Environment
Angular version: 5.2.7
Browser:
all
For Tooling issues:
- Node version: XX
- Platform:
Others:
Activity
cuiliang commentedon Mar 12, 2018
Hi @briancodes
There are 3 type of params. What you mean is query parameters, but this is Optional Parameters.
See this
And this link will work if there is no empty path segment.
briancodes commentedon Mar 13, 2018
@cuiliang The [routerLink] input in a template cannot accept query parameters.
<a [routerLink]="[{param1: 'test'}]">will cause an error because it only accepts a string or an array of string paths. The directive does not accept parameters in the same fashion asthis.router.navigate()cuiliang commentedon Mar 13, 2018
@briancodes It's not query paramemer, it's Optional Parameter.
The [routerLink] array can take object item as optional parameters.
You can see this answer on stackoverflow: https://stackoverflow.com/questions/44864303/sending-data-with-route-navigate-in-angular-2/44865817#44865817
briancodes commentedon Mar 13, 2018
@cuiliang My bad! Sorry, I see what you mean now
jasonaden commentedon May 3, 2018
Closing as this appears resolved.
teflonwaffles commentedon May 8, 2018
I'm facing problems related to this as well. Is there a specific pull request that fixed this? (I'd like a sense of what Angular version I need to wait for in order for this to be resolved.)
jasonaden commentedon May 9, 2018
@teflonwaffles This was closed because it appeared resolved from the above conversation. To produce the result OP was looking for, you would change the code to:
<a [routerLink] [queryParams]="{param1: 'test'}">This link will NOT cause error</a>reppners commentedon Jun 1, 2018
Just ran into this issue. It's not resolved. Optional matrix parameters will fail if empty routes are involved. Have to resort to query parameters to workaround it.
uvv001 commentedon Oct 18, 2018
Ran into this issue too. With route definition from first post can be reproduced by
Workaround:
jcairney commentedon Oct 18, 2018
I'm sorry @jasonaden, though that may even be the solution I needed, nobody is going to figure that out without coming to this bug. On that grounds I'd say this should be reopened and given a proper fix.
jcairney commentedon Oct 18, 2018
Actually what I quoted above did not work for my situation.
Related to what @reppners said, how do we change matrix parameters on the current route if empty routes are involved?
I tried:
<a [routerLink]="[{param1: 'test'}]">Still causes 'path' error</a>and
<a [routerLink]="['.',{param1: 'test'}]">Also causes 'path' error</a>and
<a [routerLink]="['../' + currentRequiredParamValueFromRouteParamsObservable,{param1: 'test'}]">Causes error when trying to navigate</a>Did this since the route to the empty path contained an :id required parameter in the last real segment that lead to the module with the empty path.devpreview commentedon Dec 12, 2018
@jasonaden Same error.
Route:
ThirdComponent link:
Error:
Reproduction: https://angular-router-error.stackblitz.io/parent/child

devpreview commentedon Dec 13, 2018
@jasonaden Is a any fix planned?
angular-automatic-lock-bot commentedon Sep 14, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.