Skip to content

Routing: Named outlets do not work properly as child routes #18271

Closed

Description

@kroeder

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

I need 2 named outlets for my navigation. For this I followed the official angular docs example and ended up having this:

1) Works fine

RouterModule.forRoot([
            { path: 'secondPage/:id', component: SecondPageComponent, outlet: 'contentSecond' },
            { path: 'firstPage', component: FirstPageComponent, outlet: 'contentFirst' }
], { useHash: true, enableTracing: true })

Which works fine.

In my case I need to put those routes in a parent route with an Authentication Guard:

2.1) Does not work as expected

RouterModule.forRoot([
    {
        canActivate: [],
        path: '',
        children: [
            { path: 'secondPage/:id', component: SecondPageComponent, outlet: 'contentSecond' },
            { path: 'firstPage', component: FirstPageComponent, outlet: 'contentFirst' }
        ]
    }
], { useHash: true, enableTracing: true })

// Using this to route inside the outlet
// this.router.navigate([{ outlets: { contentSecond: ['secondPage', pageNumber] } }]);

Which causes this

Router Event: NavigationStart
platform-browser.es5.js:1017 NavigationStart(id: 2, url: '/(contentSecond:secondPage/1)')
platform-browser.es5.js:1017 NavigationStart {id: 2, url: "/(contentSecond:secondPage/1)"}
platform-browser.es5.js:1026 

Router Event: NavigationError
platform-browser.es5.js:1017 NavigationError(id: 2, url: '/(contentSecond:secondPage/1)', error: Error: Cannot match any routes. URL Segment: 'secondPage/1')
platform-browser.es5.js:1017 NavigationError {id: 2, url: "/(contentSecond:secondPage/1)", error: Error: Cannot match any routes. URL Segment: 'secondPage/1'
    at ApplyRedirects.noMatchError (http…}
core.es5.js:1020 

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'secondPage/1'
Error: Cannot match any routes. URL Segment: 'secondPage/1'

2.2) Works but feels weird since 2.1 fails

        RouterModule.forRoot([
            {
                canActivate: [],
                path: '',
                children: [
                    { path: 'example', component: FirstPageComponent }
                ]
            }
        ], { useHash: true, enableTracing: true })

/*
Router Event: NavigationStart
platform-browser.es5.js:1017 NavigationStart(id: 2, url: '/example')
platform-browser.es5.js:1017 NavigationStart {id: 2, url: "/example"}
platform-browser.es5.js:1026 

Router Event: RoutesRecognized
platform-browser.es5.js:1017 RoutesRecognized(id: 2, url: '/example', urlAfterRedirects: '/example', state: Route(url:'', path:'') { Route(url:'', path:'') { Route(url:'example', path:'example') }  } )
platform-browser.es5.js:1017 RoutesRecognized {id: 2, url: "/example", urlAfterRedirects: "/example", state: RouterStateSnapshot}
*/

3) This works partly but fails at the end

RouterModule.forRoot([
    {
        canActivate: [],
        path: '',
        children: [
            {
                path: 'home', children: [{ path: 'firstPage', component: FirstPageComponent, outlet: 'contentFirst' }]
            },
            {
                path: 'secondPage', children: [{ path: ':id', component: SecondPageComponent, outlet: 'contentSecond' }]
            }
        ]
    }
], { useHash: true, enableTracing: true })

/*
1) Works fine
this.router.navigate([
    'secondPage', {
        outlets: { contentSecond: [pageNumber] }
    }
]);

2) Works fine
this.router.navigate([
    'home', {
        outlets: { contentFirst: ['firstPage'] }
    }
]);

3) Fails
this.router.navigate([
    'secondPage', {
        outlets: { contentSecond: [pageNumber] }
    }
]);

Router Event: ResolveStart
platform-browser.es5.js:1017 ResolveStart(id: 4, url: '/secondPage/(contentSecond:1)', urlAfterRedirects: '/secondPage/(contentSecond:1)', state: Route(url:'', path:'') { Route(url:'', path:'') { Route(url:'secondPage', path:'secondPage') { Route(url:'1', path:':id') }  }  } )
platform-browser.es5.js:1017 ResolveStart {id: 4, url: "/secondPage/(contentSecond:1)", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
platform-browser.es5.js:1026 

Router Event: ResolveEnd
platform-browser.es5.js:1017 ResolveEnd(id: 4, url: '/secondPage/(contentSecond:1)', urlAfterRedirects: '/secondPage/(contentSecond:1)', state: Route(url:'', path:'') { Route(url:'', path:'') { Route(url:'secondPage', path:'secondPage') { Route(url:'1', path:':id') }  }  } )
platform-browser.es5.js:1017 ResolveEnd {id: 4, url: "/secondPage/(contentSecond:1)", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
platform-browser.es5.js:1026 

Router Event: NavigationError
platform-browser.es5.js:1017 NavigationError(id: 4, url: '/secondPage/(contentSecond:1)', error: Error: Cannot activate an already activated outlet)
platform-browser.es5.js:1017 NavigationError {id: 4, url: "/secondPage/(contentSecond:1)", error: Error: Cannot activate an already activated outlet
    at RouterOutlet.activateWith (http://localhos…}
core.es5.js:1020 

ERROR Error: Uncaught (in promise): Error: Cannot activate an already activated outlet
Error: Cannot activate an already activated outlet

Again, this feels odd since routing back and forth is what I did in 1) and it worked without any errors

Expected behavior

2.1) should behave like 1) since 2.2) behaves like 1) and 3) should work, but does not

Minimal reproduction of the problem with instructions

Use code snippets from above and play around with this example
http://plnkr.co/edit/4XHwrIirGojj0fAgt5yn?p=preview

What is the motivation / use case for changing the behavior?

Make named outlets work as expected

Environment


Angular version: 4.3.1


Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: v8.0.0  
- Platform:  Windows

Others:

Edit: accidentally posted empty issue.. closed it, wrote the issue and re-opened it.

Activity

  1. changed the title [-]Routing: Named outlets do not work properly with route guards [/-] [+]Routing: Named outlets do not work properly as child routes[/+] on Jul 21, 2017
  2. BrsJsk commented on Aug 22, 2017

    @BrsJsk

    Same problem. All my routes are child routes. I can navigate them to the default page, but not to the named outlet.

  3. bennadel commented on Oct 4, 2017

    @bennadel

    I believe I am running into the same issue. I have a named outlet that is a child of a path-less component. I have a Plunkr for it. The primary outlet seems to work (Layout A / B); but, the sibling named-outlet is breaking with an unexpected error message, Error: Cannot match any routes. URL Segment: 'layout-a'.

    Plunkr: https://plnkr.co/edit/3HLVIYDdxycups61L8TA

  4. ramzgt commented on Nov 13, 2017

    @ramzgt

    Anyone figure out this issue? I am running into the same thing.

  5. abattoiry commented on Nov 30, 2017

    @abattoiry

    same problem, child empty-path named route cant override parent's. For example:

    { path: '', component: HeaderComponent, outlet: 'header', },
    {
    path: 'about',
    children: [
    { path: '', component: AboutComponent },
    { path: '', component: EmptyHeaderComponent, outlet: 'header', },
    ]
    },

    which causes error: Cannot activate an already activated outlet.

  6. jwuliger commented on Jan 22, 2018

    @jwuliger

    I can report this happening to me as well. I am using Angular 6.0.0-beta.0

  7. added this to the Backlog milestone on Jan 23, 2018
  8. nickwinger commented on Mar 14, 2018

    @nickwinger

    This is a major issue and should not be in the Backlog...

  9. RobViableIs commented on Mar 15, 2018

    @RobViableIs

    Has anyone found any solution?

  10. 13 remaining items

  11. gotwig commented on Aug 6, 2019

    @gotwig

    for 2 years -are you kidding me?

  12. loroz commented on Sep 5, 2019

    @loroz

    Hi there, still have that problem.

    Child named outlet throws error
    Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'suboutlet'
    Error: Cannot match any routes. URL Segment: 'suboutlet'

    Stackblitz example here https://stackblitz.com/edit/angular-6eip3n

    The problem is tied to the fact that the RouterModule's 'routes' is configured with an empty path:

    const routes: Routes = [{
      path: '', // <-- This is empty. This is the problem.
      component: PageComponent,
      children: [{
        path: 'subpage',
        component: SubpageComponent
      }, {
        path: 'suboutlet',
        component: SuboutletComponent,
        outlet: 'sub'
      }]
    }]
    

    Give the root path a name:

    const routes: Routes = [{
      path: 'main', // <-- Given a name 
      component: PageComponent,
      children: [{
        path: 'subpage',
        component: SubpageComponent
      }, {
        path: 'suboutlet',
        component: SuboutletComponent,
        outlet: 'sub'
      }]
    }]
    

    Then adjust your links in AppComponent's template by adding 'main' to the links:

     <a [routerLink]="['main/subpage']">Go to page</a> // <-- "main" has been added (originally was: ['subpage'])
      <a [routerLink]="['main', {outlets: {sub: ['suboutlet']}}]">Go to page with outlet</a> // <-- "main" has been added (originally was: '')
    

    After making those changes, your StackBlitz example works properly:

    image

  13. eddietisma commented on Oct 24, 2019

    @eddietisma

    +1

    Same issue with Angular 8.2.8

  14. DaSchTour commented on Jan 27, 2020

    @DaSchTour

    Seams still not being fixed with angular 9.
    Just posting to ensure that this is not closed like #10726

  15. LordRembo commented on Feb 5, 2020

    @LordRembo

    Same issue as #12740 as well I think? That one was closed but still doesn't work

    I documented the 2 workarounds in this StackOverflow post in case anyone else is stuck on it:
    https://stackoverflow.com/questions/41054333/angular-2-how-to-redirect-route-to-child-route-with-named-outlet/60072126#60072126

  16. nt-michael commented on Mar 3, 2020

    @nt-michael

    I managed to bypass this by placing the auxiliary outlet right inside the child component then creating an empty child route and it works.
    Here's my code

    const routes: Routes = [
      {
        path:'admin',
        component:AdminIndexComponent,
        children: [
          {
            path:'',
            redirectTo:'home',
            pathMatch:'full',
          },
          {
            path:'home',
            component:HomeComponent,
            children: [
              {
                path:'',
                component:NavbarHomeComponent,
                outlet:'menu'
              }
            ]
          },
          {
            path:'login',
            component:LoginComponent,
            children: [
              {
                path:'',
                component:NavbarOffComponent,
                outlet:'menu'
              }
            ]
          },
        ]
      },
    ];
    

    for instance my
    path: 'home', component:HomeComponent
    is as follows:

    <router-outlet name="menu"></router-outlet>
    <div>
      <!-- Some content -->
    </div>
    
    

    hope my approach helps ;)

  17. atscott commented on May 27, 2020

    @atscott
    Contributor

    Duplicate of #10726. That issue has been re-opened, as the fix was reverted.

  18. SamOrozco commented on Jun 14, 2020

    @SamOrozco

    @albanx The second example you gave worked for me.

  19. angular-automatic-lock-bot commented on Jul 15, 2020

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  20. locked and limited conversation to collaborators on Jul 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions