Angular 7-浏览器刷新始终重定向到主页 [英] Angular 7 - Browser Refresh always redirecting to homepage

查看:61
本文介绍了Angular 7-浏览器刷新始终重定向到主页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刷新浏览器后将用户保留在同一页面上的最佳方法是什么.

What's the best way to retain the user on same page after browser refresh.

示例:用户位于 www.domain.com/page1 中.当他刷新浏览器时,路由器将重定向到默认路由 www.domain.com/home

Example: User is in www.domain.com/page1. When he refreshes the browser, the router redirects to www.domain.com/home which is the default routing.

我可以通过使用 HashLocationStrategy 来实现.但它会在网址中添加其他#.例如: www.domain.com/#/page1 ,我想避免.

I can achieve this by using HashLocationStrategy. But it adds additional # in the url. Ex: www.domain.com/#/page1, which I want to avoid.

const routes: Routes = [
  {
    path: "page1",
    component: page1Component
  },
  { 
    path: '',
    redirectTo: 'home', 
    pathMatch: 'full'
  }

];

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

此外,我已经按照 https://angular.io/guide进行了必需的apache Web服务器配置/deployment

我知道angular是SPA,并且在root用户之后一切都是虚拟的.但是,如果没有#,还有更好的方法吗?

I understand angular is SPA and after root everything is virtual. But still, is there a better way to achieve this without #?

推荐答案

根据我对问题的理解,您可以考虑为每个具有的功能使用延迟加载功能模块.

From my understanding of your question, you can consider using lazy loading feature modules for each feature you have.

有关更多详细信息,请参阅我的 stackblitz .

See my my stackblitz for more details.

刷新stackblitz页面时,它会停留在同一页面上.

When you refresh the stackblitz page, it stay on the same page.

这篇关于Angular 7-浏览器刷新始终重定向到主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆