angular - Angular - 应用程序再次将我重定向回登录页面
问题描述
目前,我正在使用 Angular-7 开发一个 Web 应用程序。我有登录和主页。登录页面位于主页之前:
登录组件.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();
}
}
}
应用程序路由.module.ts
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]
},
];
当我单击登录页面上的提交时,我希望应用程序将我带到主页。而是,它再次重定向到登录页面。
我该如何解决这个问题?
解决方案
我不确定,但我认为你不应该在方法中使用 window.location.reload() 代码......
请删除并尝试它是否有效...
推荐阅读
- apache-kafka - 在 docker 上为 kafka 暴露端口 8083 会出错
- maven - 有没有办法修复 Ambari Web 2.7.1.0.0 构建问题?
- php - Ajax表单提交我想通过id更改表单元素的值,以便脚本从php中的返回值进行下一次调用
- json - 如何从数组的响应中获取唯一数据并使用 React js 进行重定向?
- android - 如何使用 Reddit Android 应用程序中的展开/折叠动画在 BottomSheet 内创建轮播滑块
- python - 使用 Python 将数据从 Html 表单插入到 MySQL 数据库
- python - Django:一个包提供新模板标签的规范方法?
- python - 在python中使用索引从二进制文件中获取特定项目
- javascript - Ctrl 在使用 react-scroll 和 disable-scroll 时中途停止动画滚动
- twitter - 我从免费的 Twitter API 中获得了多少百分比的推文?