angular - 角度路由始终返回默认页面
问题描述
我正在修复一个充满惊喜的旧项目。其中之一是“登录”后的刷新。它将转到''(空)上重定向登录,登录成功后它将转到主。它实际上会在微秒内闪烁,然后重定向到 '' -> empty -> 再次登录。我一直在寻找错误的路由和 window.href =... (是的,他们在某些地方使用)。我看不到它始终进入登录页面的逻辑。使用 href = "/main" 它将保留在主页上,并且不再发生“刷新”。
SigninGuard 正在正常工作。
任何想法看什么或尝试什么?
我的路由:
export const appRoutes: Routes = [
{
path: 'main',
component: MainPageComponent,
canActivate: [SigninGuard],
data: {
title: 'Home page',
subtitle: ''
}
},
{
path: 'signin',
component: SigninComponent,
data: {
title: 'Login',
subtitle: ''
}
},
{
path: 'report',
component: ReportComponent,
canActivate: [SigninGuard],
data: {
title: 'Status Report',
}
},
{
path: '',
redirectTo: 'signin', // renaming this to signinHERE proved that it always goes here
pathMatch: 'full'
},
{
path: '**',
component: NotFoundComponent,
data: { title: '404' }
}
];
签到基本上是:
signIn(name, pass) {
console.log("****** initiated");
const credentials: IAccount = { UserName: name, UserPassword: pass };
var result = this.loginService.login(credentials).subscribe(
result =>
{
if(!result.ok) {
window.location.href = '/signin2';
} else {
// save user name etc
this.router.navigate([ '/home']);
}
}
解决方案
尝试在该行添加断点:
if(!result.ok)
并检查从您的订阅返回的结果对象的值,以及结果中是否定义了属性“ok”。如果未定义,则 result.ok 将返回 false。
您还将 href 设置为“/signin2”,它不在您的路由路径中。会使用 router.navigate 重定向到您的登录页面吗?
if(!result.ok) {
this.router.navigate(['/signin']);
推荐阅读
- c++ - 如何在 Opengl 中围绕世界轴而不是局部轴旋转对象?
- heatmap - 如何使用 Echarts 将边框宽度应用于 Heatmap 中的数据项?
- unix - AWK - 在查找文件中查找数据并打印“成功”或“失败”
- swift - SwiftUI 符合 ObservableObject 的 Classes 应该是 Singleton 吗?
- java - 如何使用扫描仪类编写程序应该只打印字符串值而不是 int 值
- azure - 跨租户使用 AAD 服务到服务身份验证
- c# - 无法通过酱实验室虚拟测试中的酱存储将文件上传到移动网络应用程序
- python-3.x - Plotly:如何隐藏与气泡重叠的线条?
- java - 在托盘图标中运行应用程序(不在任务栏中)
- android - 将字节数组插入sqlite表时出现语法错误