Angular路由器从某些路线重定向到基本路线,但是当您刷新页面时,请自行修复

发布于 01-24 07:16 字数 3097 浏览 3 评论 0原文

出于某种奇怪的原因,仅对于某些路线,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

对你再特殊 2025-01-31 07:16:29

将其放入app-routing.module.ts

imports: [
    RouterModule.forRoot(
      routes,
      { enableTracing: true }
    )
]

该应用程序将记录路由行为,以便您可以调查问题。

对我来说,我认为问题围绕奇怪的问题#1 ,可能是路由器功能引起的。 router.navigaterouter.navigatebyurl的操作并非全部相同。

Put this in app-routing.module.ts

imports: [
    RouterModule.forRoot(
      routes,
      { enableTracing: true }
    )
]

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 and router.navigateByUrl weren't be all the same.

弥枳 2025-01-31 07:16:29

这确实更适合作为评论,但是当我看到在前端应用中闪烁时,这是由于两件事之一:

  1. 应用程序或组件状态的某些成员会影响渲染完成加载(页面开始加载 - &gt;挂钩更新状态 - &gt;页面开始渲染 - &gt;在渲染完成之前 - &gt; page再次开始渲染的状态更新)
  2. 在内部触发一系列重定向的东西,

我知道它并不多。答案,但我建议您仔细检查排行榜和成就组件。如果可能的话,也许添加一个日志语句详细介绍组件状态。任何共享的逻辑或子组件似乎都是调试的好候选人。

如果您还没有准备好,请尝试打开开发工具并监视网络选项卡,以排除重定向选项。

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:

  1. Some member of the app or component state that influences the rendering is updating before the page can finish loading ( page starts to load -> hook updates state -> page starts rendering -> state updates before rendering completes -> page starts rendering over again)
  2. Something is triggering a cascade of redirects internally

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.

纵性 2025-01-31 07:16:29

您不应使用pathmatch:'Full'到处都是,它的效果不佳(尤其是如果您有嵌套路由)。 docs 为您提供最常见的用法示例。通常是重定向空字符串路由以避免其他问题是个好主意。

这样的事情应该有效:

const routes: Routes = [
        {
            path: '',
            pathMatch: 'full',
            redirectTo: 'dashboard',
        },
        {
            path: 'dashboard',
            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 },
    ];

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:

const routes: Routes = [
        {
            path: '',
            pathMatch: 'full',
            redirectTo: 'dashboard',
        },
        {
            path: 'dashboard',
            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 },
    ];
雪若未夕 2025-01-31 07:16:29

刷新后,您的JWT令牌会怎样?也许您在某个过程中失去了JWT?

但是从您的描述中,看起来像Canactivatelogin中启动,也许我们发现用户已登录,login component by默认将您移至/

Canactivate应始终返回布尔值true/false,但是在这里我们只是naviage用户:

        if (!isLoggedIn) {
            console.log('Returning to login screen');
            this.router.navigateByUrl('login');
        }
@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 false;
        } else {
           return true;        
        }

    }
}

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 to login where, perhaps, we find that user is logged in and login component by default moves you to /.

canActivate should always return boolean true/false, but here we just naviage user away:

        if (!isLoggedIn) {
            console.log('Returning to login screen');
            this.router.navigateByUrl('login');
        }
@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 false;
        } else {
           return true;        
        }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文