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

查看:66
本文介绍了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 来实现这一点.但它在 url 中添加了额外的 #.例如: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 服务器配置/部署

我知道 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天全站免登陆