Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested parameterized routes #17

Closed
ghost opened this issue May 31, 2018 · 3 comments
Closed

Nested parameterized routes #17

ghost opened this issue May 31, 2018 · 3 comments

Comments

@ghost
Copy link

ghost commented May 31, 2018

Hi there!!
Based on your example:

const routes: Routes = [
{
path: '/ninja',
module: NinjaModule,
children: [
{
path: '/cats',
module: CatsModule,
},
{
path: '/dogs',
module: DogsModule,
},
],
},
];

Is it possible a request like this: /ninja/01008/cats or this /ninja/n01/dogs ?

Thanks for your work!

@shekohex
Copy link
Member

The RouterModule is just adding a prefixes to the all Controllers registered at that Module, so for Example
if we have that route:

{
    path: 'ninja', // the prefix slash is optional
    module: NinjaModule
}

and we registered NinjaController at NinjaModule

...
@Controller(':ninjaId')
export class NinjaController {
    @Get()
    findNinja(@Param('ninjaId') ninjaId) {
        return { ninjaId };
    }
}

so if we hit /ninja/10 it will get { ninjaId: 10 }.
that's have been said.

back to your question, we need to achieve this /ninja/10/cats right ?

in my opinion, each controller - NinjaController & CatsController - thay don't know each others, so instead of defining the param route in the controller scope, you need to define it on the RouterModule schema.
so instead of

{
    path: 'ninja`
    module: NinjaModule,
    children: [{ path: 'cats', module: CatsModule }, ...]
}

you will need to make it like this

{
    path: 'ninja/:ninjaId`
    module: NinjaModule,
    children: [{ path: 'cats', module: CatsModule }, ...]
}

that way, the Controllers registered at CatsModule in our case it is CatsController will be /ninja/:ninjaId/cats, instead of /ninja/cats which was on the first case, also you will need to remove :ninjaId from @Controller(':ninjaId') at NinjaController we don't need it anymore.

and now both NinjaController and CatsController will have access to ninjaId parameter.

give it a shot and let me know if there is any problem 😀

@ghost
Copy link
Author

ghost commented May 31, 2018

Great!
Thank you very much! :)

@jrista
Copy link

jrista commented Feb 15, 2019

So, in the previous example, you moved :ninjaId from the controller to the route. What if you need to be able to access ninjas both individually, by id, as well as the whole list of ninjas? Such as:

GET /ninjas -> [{ninjaId: ...}, {ninjaId: ...}]
GET /ninjas/1 -> {ninjaId: 1}

Can this be achieved with this router module, without having to hack the route for /ninjas/:ninjaId to return a list only if ninjaId is falsy?

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants