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

前端 未结 3 1969
忘了有多久
忘了有多久 2020-12-09 10:30

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\'.

W

3条回答
  •  佛祖请我去吃肉
    2020-12-09 11:15

    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();
        });
      }
    }
    

提交回复
热议问题