Angular 4: 路由器不重定向
Angular 4: Router not redirecting
我的 angular 应用程序导航有问题。当我点击登录时,它正在登录,但没有重定向到聊天页面。
这是route.ts
import ...
export const appRoutes: Routes = [
{ path: 'signup', component: SignupFormComponent},
{ path: 'login', component: LoginFormComponent},
{ path: 'chat', component: ChatroomComponent},
{ path: '', redirectTo: '/login', pathMatch: 'full'},
];
这是app.module.ts
imports: [
RouterModule.forRoot(appRoutes),
],
并登录-form.ts
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../services/auth.service';
import { Router } from '@angular/router';
@Component({
...
})
export class LoginFormComponent{
email: string;
password: string;
errorMsg: string;
constructor(private authService: AuthService, private router: Router) { }
login() {
this.authService.login(this.email, this.password)
.then(resolve => this.router.navigate(['/chat']))
.catch(error => this.errorMsg = error.message);
}
}
来自您的评论:
login(email: string, password: string) {
return this.afAuth.auth.signInWithEmailAndPassword(email, password)
.then((resolve) => {
const status = 'online'; this.setUserStatus(status);
this.router.navigate(['chat']);
});
}
this.router.navigate(['chat']);
行可能会阻止 promise 解析您的组件登录(因为 router
被无效路由阻止。)
或者,将那行从 authService
函数中取出,并允许组件 login
解析来处理它,或者,记得在 chat
之前添加斜线`authService 版本。
this.router.navigate(['/chat']);
我的 angular 应用程序导航有问题。当我点击登录时,它正在登录,但没有重定向到聊天页面。
这是route.ts
import ...
export const appRoutes: Routes = [
{ path: 'signup', component: SignupFormComponent},
{ path: 'login', component: LoginFormComponent},
{ path: 'chat', component: ChatroomComponent},
{ path: '', redirectTo: '/login', pathMatch: 'full'},
];
这是app.module.ts
imports: [
RouterModule.forRoot(appRoutes),
],
并登录-form.ts
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../services/auth.service';
import { Router } from '@angular/router';
@Component({
...
})
export class LoginFormComponent{
email: string;
password: string;
errorMsg: string;
constructor(private authService: AuthService, private router: Router) { }
login() {
this.authService.login(this.email, this.password)
.then(resolve => this.router.navigate(['/chat']))
.catch(error => this.errorMsg = error.message);
}
}
来自您的评论:
login(email: string, password: string) {
return this.afAuth.auth.signInWithEmailAndPassword(email, password)
.then((resolve) => {
const status = 'online'; this.setUserStatus(status);
this.router.navigate(['chat']);
});
}
this.router.navigate(['chat']);
行可能会阻止 promise 解析您的组件登录(因为 router
被无效路由阻止。)
或者,将那行从 authService
函数中取出,并允许组件 login
解析来处理它,或者,记得在 chat
之前添加斜线`authService 版本。
this.router.navigate(['/chat']);