Typescript 在应用程序加载之前将应用程序重定向到外部Url:6

Typescript 在应用程序加载之前将应用程序重定向到外部Url:6,typescript,angular6,microservices,angular-routing,Typescript,Angular6,Microservices,Angular Routing,因此,在应用程序组件上,我检查用户是否经过身份验证,如果没有,则将用户重定向到具有登录页面的rest api调用。 当前代码在app.component.ts的构造上具有api重定向调用 所以发生的是, app.component的html页面加载1秒,然后发生重定向。 在屏幕上加载任何html(DOM元素)之前,如何将用户重定向到rest api url?您可以检查用户是否使用 例如: app.module.ts const appRoutes: Routes = [{ path: '

因此,在应用程序组件上,我检查用户是否经过身份验证,如果没有,则将用户重定向到具有登录页面的rest api调用。 当前代码在app.component.ts的构造上具有api重定向调用 所以发生的是, app.component的html页面加载1秒,然后发生重定向。
在屏幕上加载任何html(DOM元素)之前,如何将用户重定向到rest api url?您可以检查用户是否使用

例如:

app.module.ts

const appRoutes: Routes = [{
    path: '',
    component: HomelayoutComponent,
    canActivate: [LoginGuardService]
}]
您的登录名为-guard.service.ts

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
 // Implement here your auth check logic
  if (localStorage.getItem('jwtToken')) {
    return true;
  }

  // not logged in so redirect to login page with the return url and return false
  this.router.navigate(['login'], {
    queryParams: {
      returnUrl: state.url
    }
  });
  return false;
}

您可以检查用户是否已使用登录您的路由

例如:

app.module.ts

const appRoutes: Routes = [{
    path: '',
    component: HomelayoutComponent,
    canActivate: [LoginGuardService]
}]
您的登录名为-guard.service.ts

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
 // Implement here your auth check logic
  if (localStorage.getItem('jwtToken')) {
    return true;
  }

  // not logged in so redirect to login page with the return url and return false
  this.router.navigate(['login'], {
    queryParams: {
      returnUrl: state.url
    }
  });
  return false;
}