【问题标题】:How to stay in the same angular page after a page refresh?页面刷新后如何保持在同一个角度页面?
【发布时间】:2020-02-12 11:08:44
【问题描述】:

当我刷新我的 Angular 网页时,Angular 应用程序会自动将我带到主页,当我复制和粘贴 URL 时,Angular 应用程序会自动转到主页。

我想在刷新页面或将 URL 粘贴到地址栏中后留在同一页面。

我正在使用带有 AdalGuard 类的 angular 7

这是路由代码

  { path: '', component: HomeComponent, canActivate: [AdalGuard] },
  { path: 'meal/:id', component: MealDetails, , canActivate: [AdalGuard]},
  { path: 'coldmeal/:id/:page', component: ColdMealComponent, canActivate: [AdalGuard]},  
  { path: '**', component: NotFoundComponent }
];
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

我的 app.module.ts 有 HashLocationStrategy:

providers: [ { provide: LocationStrategy, useClass: HashLocationStrategy } ]

我希望在页面刷新后或在地址网络栏中粘贴 URL 后留在同一页面。

谢谢。

【问题讨论】:

  • 请检查这是否不是您需要的解决方案!如果不是,我会尽力帮助更多codecraft.tv/courses/angular/routing/routing-strategies/…
  • 嗨 @FranciscoSantorelli 我正在使用 HashLocationStrategy 并且我的 app.module.ts 看起来像:providers: [ { provide: LocationStrategy, useClass: HashLocationStrategy } ] 但它仍然无法正常工作
  • 你尝试过使用cookies吗?
  • @PatoVargas 我不确定 cookie 是否会导致该问题。有什么建议我可以使用 cookie 来修复它吗?

标签: angular redirect routing


【解决方案1】:

我通过在 app.module.ts 文件中用 PathLocationStrategy 替换 HashLocationStrategy 解决了这个问题。

providers: [ { provide: LocationStrategy, useClass: PathLocationStrategy } ]

现在我可以刷新页面而不会丢失它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 2019-05-19
    • 2016-06-20
    • 2023-03-28
    • 2021-08-21
    • 2019-09-19
    • 1970-01-01
    相关资源
    最近更新 更多