Angular路由器从某些路线重定向到基本路线,但是当您刷新页面时,请自行修复
出于某种奇怪的原因,仅对于某些路线,Angular路由器将用户重定向到基本URL。
听起来我的app-routing.module.ts是一个问题吗?
好吧,这是奇怪的问题开始的地方。
奇怪的问题#1
仅在试图导航到/sterdegboard
和/Achievements
时才发生>/team-view
奇怪的问题#2
如果我对代码进行更改或简单地刷新页面,则现在解决了问题,我现在可以同时访问/排行榜
和/成就
没有任何问题。如果我注销并登录,问题将返回。
奇怪的问题#3
除了页面外,没有控制台错误或任何形式的错误,只是很快地闪烁,然后将我重定向到/
路由(这是仪表板组件
loads`)
路由:
const routes: Routes = [
{
path: '',
pathMatch: 'full',
component: DashboardComponent,
canActivate: [LoggedInGuard],
},
{
path: 'team-view',
component: TeamViewComponent,
canActivate: [LoggedInGuard],
},
{
path: 'settings',
component: SettingsComponent,
canActivate: [LoggedInGuard],
},
{
path: 'leaderboard',
component: LeaderboardComponent,
canActivate: [LoggedInGuard],
},
{
path: 'achievements',
component: AchievementsComponent,
canActivate: [LoggedInGuard],
},
{ path: 'register', component: RegisterComponent },
{ path: 'login', component: LoginComponent },
];
标题代码:
<div class="menu-wrapper desktop-menu">
<span class="menu-item" routerLinkActive="menu-item-active" [routerLinkActiveOptions]="{ exact: true }" routerLink="/">Dashboard</span>
<span class="menu-item" routerLinkActive="menu-item-active" [routerLinkActiveOptions]="{ exact: true }" routerLink="/leaderboard">Leaderboard</span>
<span class="menu-item" routerLinkActive="menu-item-active" [routerLinkActiveOptions]="{ exact: true }" routerLink="/achievements">Achievements</span>
<span class="menu-item" routerLinkActive="menu-item-active" [routerLinkActiveOptions]="{ exact: true }" routerLink="/team-view">My Team</span>
<span class="menu-item" routerLinkActive="menu-item-active" (click)="ToggleNotifications()"><mat-icon matBadge="4">notifications</mat-icon></span>
<span class="menu-item" routerLinkActive="menu-item-active" (click)="SignOut()"><mat-icon>logout</mat-icon></span>
</div>
loggedinginguard:
@Injectable()
export class LoggedInGuard implements CanActivate {
constructor(private router: Router) {}
canActivate(): Observable<boolean> | Promise<boolean> | boolean {
const isLoggedIn = localStorage.getItem('jwt') !== null;
if (!isLoggedIn) {
console.log('Returning to login screen');
this.router.navigateByUrl('login');
}
return isLoggedIn;
}
}
我还注意到在角路由中似乎有些怪异的日志记录,这些路由似乎表明Angular Router正在强制重定向,但我不强迫知道为什么吗?:
GuardsCheckEnd(id: 11, url: '/', urlAfterRedirects: '/', state: Route(url:'', path:'') { Route(url:'', path:'') } , shouldActivate: true)
For some strange reason, only for certain routes, Angular Router is redirecting the user back to the base url.
Sounds like a problem with my app-routing.module.ts right?
Well this is where the strange issues start.
Strange Issue #1
Only happens when trying to navigate to /leaderboard
and to /achievements
but not on /team-view
Strange Issue #2
If I make a change to the code OR simply refresh the page, the problems are now resolved and I can now reach both /leaderboard
and /achievements
without any problems. If I log out and log back in, the problem returns.
Strange Issue #3
There are no console errors or any form of error aside from the page just flickering very quickly and redirecting me back to the /
route (which is the Dashboard Component
loads`)
Routes:
const routes: Routes = [
{
path: '',
pathMatch: 'full',
component: DashboardComponent,
canActivate: [LoggedInGuard],
},
{
path: 'team-view',
component: TeamViewComponent,
canActivate: [LoggedInGuard],
},
{
path: 'settings',
component: SettingsComponent,
canActivate: [LoggedInGuard],
},
{
path: 'leaderboard',
component: LeaderboardComponent,
canActivate: [LoggedInGuard],
},
{
path: 'achievements',
component: AchievementsComponent,
canActivate: [LoggedInGuard],
},
{ path: 'register', component: RegisterComponent },
{ path: 'login', component: LoginComponent },
];
Header Code:
<div class="menu-wrapper desktop-menu">
<span class="menu-item" routerLinkActive="menu-item-active" [routerLinkActiveOptions]="{ exact: true }" routerLink="/">Dashboard</span>
<span class="menu-item" routerLinkActive="menu-item-active" [routerLinkActiveOptions]="{ exact: true }" routerLink="/leaderboard">Leaderboard</span>
<span class="menu-item" routerLinkActive="menu-item-active" [routerLinkActiveOptions]="{ exact: true }" routerLink="/achievements">Achievements</span>
<span class="menu-item" routerLinkActive="menu-item-active" [routerLinkActiveOptions]="{ exact: true }" routerLink="/team-view">My Team</span>
<span class="menu-item" routerLinkActive="menu-item-active" (click)="ToggleNotifications()"><mat-icon matBadge="4">notifications</mat-icon></span>
<span class="menu-item" routerLinkActive="menu-item-active" (click)="SignOut()"><mat-icon>logout</mat-icon></span>
</div>
LoggedInGuard:
@Injectable()
export class LoggedInGuard implements CanActivate {
constructor(private router: Router) {}
canActivate(): Observable<boolean> | Promise<boolean> | boolean {
const isLoggedIn = localStorage.getItem('jwt') !== null;
if (!isLoggedIn) {
console.log('Returning to login screen');
this.router.navigateByUrl('login');
}
return isLoggedIn;
}
}
I also noticed some weird logging in the Angular Routing that seems to indicate that Angular Router is forcing a redirect but I don't know why?:
GuardsCheckEnd(id: 11, url: '/', urlAfterRedirects: '/', state: Route(url:'', path:'') { Route(url:'', path:'') } , shouldActivate: true)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将其放入
app-routing.module.ts
该应用程序将记录路由行为,以便您可以调查问题。
对我来说,我认为问题围绕奇怪的问题#1 ,可能是路由器功能引起的。
router.navigate
和router.navigatebyurl
的操作并非全部相同。Put this in
app-routing.module.ts
The application will log out route behavior so you can investigate the issue.
For me, I think the problem is around Strange Issue #1, it can be cause by router function. Actions of
router.navigate
androuter.navigateByUrl
weren't be all the same.这确实更适合作为评论,但是当我看到在前端应用中闪烁时,这是由于两件事之一:
我知道它并不多。答案,但我建议您仔细检查排行榜和成就组件。如果可能的话,也许添加一个日志语句详细介绍组件状态。任何共享的逻辑或子组件似乎都是调试的好候选人。
如果您还没有准备好,请尝试打开开发工具并监视网络选项卡,以排除重定向选项。
This is really more appropriate as a comment, but when I've seen flickering in a front end app like that it was due to one of two things:
I know it's not much of an answer, but I would recommend double checking the leaderboard and achievements components. If possible maybe add a log statement detailing component state. Any shared logic or sub-components seem like a good candidate for debugging.
If you haven't all ready, try opening dev tools and monitoring the network tab just to rule out the redirect option.
您不应使用
pathmatch:'Full'
到处都是,它的效果不佳(尤其是如果您有嵌套路由)。 docs 为您提供最常见的用法示例。通常是重定向空字符串路由以避免其他问题是个好主意。这样的事情应该有效:
You should not use
pathMatch: 'full'
everywhere, it does not play well (especially if you have nested routes). The docs give you examples of the most common usages. It is usually a good idea to redirect empty string routes to avoid other issues.Something like this should work:
刷新后,您的JWT令牌会怎样?也许您在某个过程中失去了JWT?
但是从您的描述中,看起来像
Canactivate
在login
中启动,也许我们发现用户已登录,login
component by默认将您移至/
。Canactivate
应始终返回布尔值true/false,但是在这里我们只是naviage用户:What happens with your jwt token after you refresh? Maybe you lose jwt during some process?
But from your description seems like
CanActivate
kicks in forces you tologin
where, perhaps, we find that user is logged in andlogin
component by default moves you to/
.canActivate
should always return boolean true/false, but here we just naviage user away: