angular7childrenrouter-outletrouterlink

Angular 7 - Multiple outlets : Error: Cannot activate an already activated outlet


Here is the issue that I'm encountering with Angular 7 :

I have two outlets : the main app router outlet, and a secondary outlet named 'administration'.

When I want to navigate through any administration link at start, it works fine. But next time, when I try to navigate, angular throws this error message :

Error: Cannot activate an already activated outlet

So, can someone explain me why ? I haven't found any solution on forums...

Here is a stackblitz : https://stackblitz.com/edit/angular-osnnd4

Thank you all everybody :)


Solution

  • The problem occurs when lazyloading child routes. You have to manually deactivate the outlet everytime you change a route.

    I have modified your AdministrationComponent to workaround as follow. It should be able to work for now, until Angular have a way to solve the problem.

    import { Component, OnInit, ViewChild } from '@angular/core';
    import { RouterOutlet, Router, ActivationStart } from '@angular/router';
    
    @Component({
      selector: 'app-administration',
      templateUrl: './administration.component.html',
      styleUrls: ['./administration.component.css']
    })
    export class AdministrationComponent implements OnInit {
    
      @ViewChild(RouterOutlet) outlet: RouterOutlet;
    
      constructor(
        private router: Router
      ) { }
    
      ngOnInit(): void {
        this.router.events.subscribe(e => {
          if (e instanceof ActivationStart && e.snapshot.outlet === "administration")
            this.outlet.deactivate();
        });
      }
    }