Angular-应用程序再次将我重定向回登录页面

Angular-应用程序再次将我重定向回登录页面,angular,Angular,目前,我正在使用Angular-7开发一个web应用程序。我有登录和主页。登录页面位于主页之前: login.component.ts import { Component, OnInit } from '@angular/core'; import { ApiService } from '../../../shared/services/api.service'; import { TokenService } from '../../../shared/services/token.ser

目前,我正在使用Angular-7开发一个web应用程序。我有登录和主页。登录页面位于主页之前:

login.component.ts

import { Component, OnInit } from '@angular/core';
import { ApiService } from '../../../shared/services/api.service';
import { TokenService } from '../../../shared/services/token.service';
import { Router, Route } from '@angular/router';
import { AuthService } from '../../../shared/services/auth.service';
import { SnotifyService } from 'ng-snotify';
import { Ng4LoadingSpinnerService } from 'ng4-loading-spinner';

declare var $;

@Component({
 selector: 'app-login',
 templateUrl: './login.component.html',
 styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

public loggedIn: boolean;

 public form = {
  email : null,
  password : null,
  remember_me : false
};

student = null;
name = null;
students = null;

public error = null;

constructor(
 private api: ApiService,
 private token: TokenService,
 private router: Router,
 private auth: AuthService,
 private notify: SnotifyService,
 private spinnerService: Ng4LoadingSpinnerService
 ) {}

ngOnInit() {


document.body.className = 'hold-transition login-page';
$(() => {
  $('input').iCheck({
    checkboxClass: 'icheckbox_square-blue',
    radioClass: 'iradio_square-blue',
    increaseArea: '20%' /* optional */
   });
 });
}

onSubmit(){
 this.notify.info('Wait...', {timeout: 0});
 this.spinnerService.show();
 var headers = {
   'Content-Type' : 'application/json'
 }
 return this.api.post('login', this.form, headers).subscribe(
   data => this.tokenHandler(data),
   error => this.errorHandler(error.error)
  );
 }

errorHandler(error){
  this.spinnerService.hide();
  this.notify.clear();
  console.log(error);
  if(error.errors && error.errors.email){
  this.error = error.errors.email;
}
else if(error.message=='Unauthorized'){
  this.error = null;
  this.notify.error('Invalid Login Details or email not confirmed', {timeout: 0})
} else {
  this.error = null;
  this.notify.error(error.message, {timeout:0})
}
}

tokenHandler(data){
 this.notify.clear();
 console.log(data);
 this.token.setRoles(data.user.roles);
this.token.set(data.token_type + " " + data.access_token, data);
this.auth.changeAuthStatus(true);
this.loggedIn = true;
 this.notify.info('Login Succesfully', {timeout: 2000});
 this.wait(999);
 this.router.navigateByUrl('/home');
 window.location.reload();
}

private wait(ms){
 var start = new Date().getTime();
 var end = start;
 while(end < start + ms) {
   end = new Date().getTime();
 }
}
}
当我在登录页面上单击Submit时,我希望应用程序将我带到主页。但是,它会再次重定向到登录页面


如何解决此问题?

我不确定,但我认为您不应该使用 方法中的window.location.reload()代码


请删除并尝试它是否工作

那么
AfterloginService
guard看起来怎么样?
const routes: Routes = [
{ path: '', redirectTo: '/landing', pathMatch: 'full' },
{ path: 'landing', component: LandingPageComponent },

{
 path: 'login',
 component: LoginComponent
},
{
 path: 'client-quote-landing',
 component: ClientQuotesLandingComponent
},
{
 path: 'home',
 component: HomeComponent,
 canActivate : [AfterloginService]
},

];