【问题标题】:Angular 7 navigate is changing URL accordingly BUT doesn't load the componentAngular 7 导航正在相应地更改 URL,但不加载组件
【发布时间】:2019-10-14 16:20:00
【问题描述】:

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

解释:

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

  • 如果他登陆 HOME 组件,EVERYTHING's work,他可以在应用程序的任何地方导航。

  • 1234563

我使用了延迟加载和 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])

【问题讨论】:

  • 为什么在日志函数中返回响应
  • @HitechHitesh 我的错,你说得对,我删除了它。
  • 以前没有人遇到过这个问题?

标签: angular typescript angular-router auth-guard


【解决方案1】:

我发现了我的问题。 这是因为我的<router-outlet><router-outlet> 上的*ngIf 条件。

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

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

谢谢。

【讨论】:

  • 我一直在寻找这个问题的解决方案,在这里找到了解决方案..谢谢..
猜你喜欢
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 2019-05-31
  • 2020-02-05
  • 2020-08-09
  • 2020-08-16
  • 2019-04-30
相关资源
最近更新 更多