2

I have this configuration in my routing in Angular:

...
{
          path: 'account',
          canActivate: [AuthGuardService],
          children: [
            {
              path: '',
              component: AccountPageComponent,
              outlet: 'modal',
              children: [
                {
                  path: 'pets',
                  component: PetsComponent,
                  children: [
                    {
                      path: ':id',
                      component: PetComponent,
                    }
                  ]
                },
                {
                  path: 'user',
                  component: UserComponent,
                }
              ]
            }
          ]
        }
...

Account route is working as expected and opening in desired outlet. AccountPageComponent has his own nameless outlet for this route children. But when I'm navigating to any of them I'm receiving following errors:

NG04002: Cannot match any routes. URL Segment: 'account/user'

NG04002: Cannot match any routes. URL Segment: 'account/pets'

How can I fix this?

Thanks

1 Answer 1

2

You should navigate using the outlet name - modal:

this.router.navigate(['account', { outlets: { modal: ['pets', '1'] } }]);

this.router.navigate(['account', { outlets: { modal: ['user'] } }]);

In HTML it will be:

<a [routerLink]="['account', { outlets: { modal: ['pets', '1'] } }]">Pet 1</a>
<a [routerLink]="['account', { outlets: { modal: ['user'] } }]">User</a>

The redirection might look like:

{
      path: 'account',
      canActivate: [AuthGuardService],
      children: [
        {
          path: '',
          component: AccountPageComponent,
          outlet: 'modal',
          children: [
            {
              path: 'pets',
              component: PetsComponent,
              children: [
                {
                  path: ':id',
                  component: PetComponent,
                }
              ]
            },
            {
              path: 'user',
              component: UserComponent,
            }
          ]
        },
        {
          path: '/user',
          outlet: 'modal',
          redirectTo: 'user',
          pathMatch: 'full'
        },
        {
          path: '/pets/:id',
          outlet: 'modal',
          redirectTo: (redirectData: any) => {
            return // construct redirect route with params.
          },
          pathMatch: 'full'
        },
      ]
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, your solution works. But what in case of accessing account/user or typing directly to browser? Redirecting?
@MichałB Yes, that is possible, but better to achieve using primary routes, it is more cleaner, just a suggestion.
Could you provide any simple example? I'm not so proficient at this yet.
@MichałB updated my answer with rough estimate of how the redirects will look like, please try configuring the redirect Fn for the parameterized route redirection.

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.