Angular 7 导航正在相应更改 URL 但未加载组件

Angular 7 navigate is changing URL accordingly BUT doesn't load the component

在尝试所有其他解决方案后,我仍然遇到我的问题:尝试导航到其他组件, URL 已更改但我停留在同一页面上,目标组件是不加载.

解释:

当用户到达 App 时,基于会话存储,他会转到 HOME 组件LOGIN 组件

我使用了延迟加载和 authGuard。

控制台没有错误。

上述两种情况之间的路由器跟踪日志是相同的(我的意思是在第二种情况下,NavigationEnd 组件是正确的目标组件,但它从未加载过)

这是我的 app-routing.module.ts

 const routes: Routes = [
  {
    path: '',
    redirectTo: 'login',
    pathMatch: 'full',
  },
  {
    path: 'home',
    loadChildren: './pages/home/home.module#HomeModule',
    canActivate: [AuthGuard]
  },
  {
    path: 'description',
    loadChildren: './pages/description/description.module#DescriptionModule',
    canActivate: [AuthGuard]
  },
  {
    path: 'nsp',
    loadChildren: './pages/nsp/nsp.module#NspModule',
    canActivate: [AuthGuard]
  },
  {
    path: 'login',
    loadChildren: './pages/login/login.module#LoginModule'
  },
  {
    path: 'mappings',
    loadChildren: './pages/mappings/mappings.module#MappingsModule',
    canActivate: [AuthGuard]
  },
  {
    path: 'performances',
    loadChildren: './pages/performances/performances.module#PerformancesModule',
    canActivate: [AuthGuard]
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes, {enableTracing: true })],
  exports: [RouterModule] 
})
export class AppRoutingModule { }

这是我的 auth-guard.service.ts:

export class AuthGuardService implements CanActivate {

  constructor(private storageFactory: StorageFactoryService, 
              public auth: AuthentificationService, 
              public router: Router) {}

  session_date_expire_on: string;

  canActivate(_route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {

    if (this.storageFactory.get('ssid_expires_on') != null) {
      var date_stored = 
                  new Date(this.storageFactory.get('ssid_expires_on').value);
    } 
    var current_date = new Date();

    if (typeof this.storageFactory.get('userid') !== 'undefined' &&
        this.storageFactory.get('userid') !== null && 
        date_stored > current_date) {

      this.storageFactory.remove('ssid_expires_on');
      this.session_date_expire_on = new Date(current_date.getTime() +
                                      environment.inactivity_timeout).toString();
      this.storageFactory.set('ssid_expires_on', this.session_date_expire_on);
      current_date = null;
      return true;
    }

    localStorage.clear();
    sessionStorage.clear();
    this.router.navigate(['/login']);
    return false;   
  }
}

我的 app.component.ts 直接重定向到 HOME 组件,因此调用 authGuard 并在需要时重定向到 LOGIN:

export class AppComponent implements OnInit {

constructor(private checkSessionService: CheckSessionService, 
            private storageFactory: StorageFactoryService, 
            private ElementRef: ElementRef, 
            private api_user: AuthentificationService, 
            private router: Router) { }

  ngOnInit() {
    console.log("--------- App Component ---------");
    this.router.navigate(['/home']);
  }
}

问题是当我转到 login.component.ts 并单击日志功能时,如果用户经过身份验证,它会转到主页,然后导航无法正常工作:

export class LoginComponent implements OnInit {
  user: UserInfo;
  current_date = new Date();
  session_date_expire_on: string;
  access_granted: boolean;

  constructor(private ngZone: NgZone, 
              private storageFactory: StorageFactoryService, 
              private api_user: AuthentificationService, 
              private router: Router, 
              private route: ActivatedRoute) { }

  ngOnInit() {}

  log() {
    return this.api_user.getUser().subscribe(response => {
      if (response.status == 200) {

        this.user = response.body;
        this.session_date_expire_on = new Date(this.current_date.getTime() +
                                      environment.inactivity_timeout).toString();
        this.storageFactory.set('userid', this.user.userId);
        this.storageFactory.set('usercountry', this.user.entityCountryName);
        this.storageFactory.set('userrights', this.user.profile[0]);
        this.storageFactory.set('ssid', uuid());
        this.storageFactory.set('ssid_expires_on', this.session_date_expire_on);

        this.router.navigate(['/home']);

      } else {
        this.router.navigate(['/login']);
      }
    })
  }
}

你有什么想法吗?

我已经试过了 .. --> this.router.navigate([../home])

我已经解决了我的问题。 这是因为我的 <router-outlet><router-outlet>.

条件 *ngIf

所以我的问题是一个路由器插座已注册,无论您做什么,下一个路由器插座都没有响应路由更改。

我删除了我的条件并且它起作用了。

谢谢。