angular - 页面在注销时冻结 - AngularFire
问题描述
每次我尝试注销时,页面都会冻结,并且在我点击刷新之前我无法转到另一个页面。我正在使用 Angular 和 Firebase firestore 及其身份验证功能。
编辑:
我添加了我的 app-routing.module.ts 以获取更多信息,并希望能澄清问题到底是什么。
auth.service.ts
@Injectable({
providedIn: 'root'
})
export class AuthService {
authState: any = null; // Used to hold the users data
// calls the AngularFireAuth to get info about user credentials
constructor(private afAuth: AngularFireAuth, private router: Router, public fireService: AngularFirestore) {
this.afAuth.authState.subscribe((auth => {
this.authState = auth;
}));
}
// is called when company register. creates firebase auth login with email and password
// this also creates a document with the information from the registration in the collection companies
registerWithEmail(email: string, password: string, companyUser: CompanyUserInterface) {
return this.afAuth.createUserWithEmailAndPassword(email, password).then((data) => {
this.authState = data;
// Gets user id and added to company for unique id
companyUser.UID = data.user.uid;
return this.fireService.collection('companies').add(Object.assign({}, companyUser));
}).catch(error => {
console.log(error);
throw error;
});
}
// is called in login and checks if login info is correct in firebase
loginWithEmail(email: string, password: string) {
return this.afAuth.signInWithEmailAndPassword(email, password).then((data) => {
this.authState = data;
})
.catch(error => {
console.log(error);
throw error;
});
}
// TODO: is bugged. if time look into. (has to refresh page on logout to be able to move)
logout(): void {
this.afAuth.signOut().then();
this.router.navigate(['/login']).then();
}
}
应用程序路由.module.ts
import {NgModule} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';
import {HomeComponent} from './views/home/home.component';
import {AboutComponent} from './views/about/about.component';
import {PagenotfoundComponent} from './views/pagenotfound/pagenotfound.component';
import {ContactComponent} from './views/contact/contact.component';
import {LoginComponent} from './views/login/login.component';
import {RegisterComponent} from './views/register/register.component';
import {UserinfoComponent} from './views/userinfo/userinfo.component';
import {BoardComponent} from './views/board/board.component';
import {ScheduleComponent} from './views/schedule/schedule.component';
import {TermComponent} from './views/term/term.component';
import {AngularFireAuthGuard} from '@angular/fire/auth-guard';
const routes: Routes = [
// free for all visitor
{path: 'home', component: HomeComponent},
{path: 'about', component: AboutComponent},
{path: 'contact', component: ContactComponent},
{path: 'login', component: LoginComponent},
{path: 'register', component: RegisterComponent},
{path: 'terms', component: TermComponent},
// has to be logged in to access these paths
{path: 'userinfo', component: UserinfoComponent, canActivate: [AngularFireAuthGuard]},
{path: 'board', component: BoardComponent, canActivate: [AngularFireAuthGuard]},
{path: 'schedule', component: ScheduleComponent, canActivate: [AngularFireAuthGuard]},
// other
{path: '', redirectTo: '/home', pathMatch: 'full'}, // redirect to `first-component`
{path: '**', component: PagenotfoundComponent}, // Wildcard route for a 404 page
];
// configures NgModule imports and exports
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {
}
解决方案
尝试logout()
像这样重构您的函数:
logout(): void {
this.afAuth.signOut().then(() => {
this.router.navigate(['/login']);
});
}
本质上,这不是一个接一个地调用两个异步函数,而是将它们链接起来。
推荐阅读
- android-studio - 我在 android studio 配置中遇到问题可以帮助我吗?
- flutter - Flutter:移动到上一个屏幕(2nd)并仅删除上一个屏幕(2nd 3rd 4th),而不是所有屏幕(1st 2nd 3rd 4th)
- powerbi - 有什么方法可以将 Einstein 分析连接到一些 3rd 方 BI 应用程序,例如 powerbi?
- sql - 选择连字符分隔的列到行
- authentication - 如何在另一个选项卡中使用活动的 GitHub 会话
- unity3d - 在使用 Unity 和 Oculus SDK 的 Oculus Quest 中,耳机位置始终返回零
- machine-learning - Holdout 数据集与 Validation 数据集有什么区别?
- c# - 将 JSON 反序列化为嵌套模型
- java - 如何在 Java 中的注释属性值中插入运行时环境变量?
- python - 如何从函数中更改变量值?