Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXCEPTION: Root segment cannot have matrix parameters

Tags:

angular

Html:

[routerLink]="['', {'scrollTo': '#contact'}]"

TS:

this.route.params.forEach((params: Params) => {
    if (params['scrollTo']) {
        // some code here
    }
});

Error: EXCEPTION: Root segment cannot have matrix parameters

I can't have 'scrollTo' param in my routerLink?

It clearly appears on the angular 2 documentation: https://angular.io/docs/ts/latest/guide/router.html#!#appendix-link-parameters-array

EDIT:

Doesn't seem to complaint with : <a [routerLink]="['/crisis-center', { scrollTo: '#contact' }]">Crisis Center</a>. But I need it with my '' root route.

like image 692
Gerald Hughes Avatar asked Oct 17 '16 14:10

Gerald Hughes


3 Answers

Recently, it is done this way in Angular 5+:

<a [routerLink]="['/']" [queryParams]="{ 'tour': true }"> text </a>

Doing [routerLink]="['/', { 'tour': true' }]"> text </a> would throw the error

like image 65
KhoPhi Avatar answered Oct 18 '22 18:10

KhoPhi


The accepted answer is wrong, it sets query params (e.g ?myparam=true). To set matrix parameters (e.g ;myparam=15;foo=foo) the syntax is:

[routerLink]="['.', {'scrollTo': '#contact'}]"

Notice the period in the route. As is explained here:

https://github.com/angular/angular/issues/9505

That's how to set matrix parameters, and it fails with "''" because that's for the root route, which apparently won't allow matrix parameters-

like image 25
chrismarx Avatar answered Oct 18 '22 17:10

chrismarx


The root segment cannot have matrix params. There are two reasons for this:

  • There is no place in the URL to put them.
  • Currently we reuse components when their parameters change, but in the future we will provide a way to customize the reuse behavior. So the developer will be able to get a new copy of a component any time its args change. Since the root component is created statically, we cannot implement it for the root component.
like image 2
Gerald Hughes Avatar answered Oct 18 '22 17:10

Gerald Hughes