4

my url is :

http://localhost/#/foo;nb=25;page=1

in the component linked to this url, I do :

ngOnInit() {
        this.queryParamSub = this._route.queryParams.subscribe(queryParams => {
           console.log(queryParams)
        })
}

However, I keep getting an empty object as queryParams. I did try with my url as : http://localhost/#/foo?nb=25&page=1 and it worked fine. Any idea how can I solve this issue if I want to use the matrix notation ?

EDIT this._route is ActivatedRoute

EDIT Here is my app-routing.module.ts

@NgModule({
    imports: [
        RouterModule.forChild([
            {
                path: "",
                component: AppComponent,
                children: [
                    {
                      path: "foo",
                      component: HomepageComponent,
                      data: {
                          name: 'home'
                      }
                    }
                ]
            }]
        )
    ],
    exports: [
        RouterModule
    ]
})

export class AppRoutingModule {}

The component in which I try to get the query param is HomepageComponent

1 Answer 1

2

Query parameters on the root route are serialized as query parameters ?xxx=yyy, query parameters on child routes are serialized as matrix parameters ;xxx=yyy.

So what you need to do is to add the parameters to a child route segment to get them in matrix notation.

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry mate but I am afraid I don't see what you mean by "add them to a child route segment" ...
If you have a route { path: 'root', children: [{ path: 'child', component: Child}]} and this.router.navigate(['/root', {nb:25}, 'child', {page:1}]) you should get http://localhost/#/root/child;page=1?nb=25 (at the end of angular.io/docs/ts/latest/guide/…)
Sorry, it seems I misinterpreted your question a bit. In addition to my answer which explains when matrix parameters are used, you need to apply the _route.queryParams.subscribe() at the right component, the one that was added by the route where the matrix parameters belong to. I don't know if the URL you posed is actually valid. Was this URL created from Angular by the router or did you invent it?
Can you please provide the routes configuration you're using?
Seems I am subject to some confusion here myself. I assumed optional route parameters are queryParameters, but it seems they are something different. This is also why they are passed within the array ['/root', {...}] and not in routingExtras that also allows to pass queryParameters but only in addition to the entire route not to segments.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.