0

I am trying to set up a browser to browse through a tree. Every time a leave is selected, its children can be selected.

Since I want to make generic, I only want to specify one route.

Unfortunatly, that does not really work... This works

RouterModule.forRoot( [
{ path: '', redirectTo: '/browser', pathMatch: 'full' },
{
    path: 'browser', children: [
    {
        path: '',
        component: BrowserComponent
    },
    {
        path: '**',
        component: BrowserComponent
    }
]
},

] )

But then I am not able to read the params. This prints an emtpy object

this.route.params.forEach((params: Params) => {
   console.log(params);
});

I think it should be something like this:

{ path: '**/:id', component: BrowserComponent }

But that does also not work..:/ Can somebody tell me what I'm doing wrong? Thanks in advance.

PS: In rails I would do it like this:

get 'browser/*id',
7
  • What is this.route's type? ActivatedRoute? Commented Nov 14, 2016 at 8:38
  • Yes. this.route is declared in the costructor private route: ActivatedRoute Commented Nov 14, 2016 at 9:37
  • Can you expand your route config a bit please? Commented Nov 14, 2016 at 9:47
  • I have add the complete route config. Its still very small... Commented Nov 14, 2016 at 10:52
  • The ** path is the wildcard path as you know. Are you trying to pass a parameter to the wildcard route? Commented Nov 14, 2016 at 11:57

1 Answer 1

1

Here's a plunker: http://plnkr.co/edit/kWSOa8i6095e0a5pBlgq?p=preview

In order to get the params change the routeconfig like this:

RouterModule.forRoot( [
{ path: '', redirectTo: '/browser', pathMatch: 'full' },
{
    path: 'browser', children: [
    {
        path: ':id',
        component: BrowserComponent
    },
    {
        path: '**',
        component: AnotherComponent
    }
]
},

This way, when the user goes to the url browser/1, you can see 1 in the params. I also suggest you to change the component of the wildcard route (**) because I assume you are going to use a parameter inside your BrowserComponent and if your user doesn't provide a parameter it should go to an error page.

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

7 Comments

This does only work with one parameter. browser/1/2 does not work.
@Moritz of course. You need a deeper route config for that. Not forRoot but forChildren
obviously I could add another static route. However, it should be generic since I do not know how many IDs it will be. It can be /browser, /browser/1, /browser/1/2, /browser/1/2/3... Finally, I only care about the last id. The other IDs will be used for history, e.g. breadcrumb.
Exactly, where N >= 0.
|

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.