我想在第一次加载平台时向浏览器历史记录中注入一个新的状态,这样当用户单击浏览器的后退按钮时,它应该会登陆到应用程序的主页。
我尝试使用1. PlatformLocation pushState() 2. window.history.pushState()添加新状态。
location.pushState(null, 'home', 'home');
// window.history.pushState(null, 'home', 'home');我甚至试着给出完整的URL
location.pushState(null, 'home', 'http://localhost:4200/home');
// window.history.pushState(null, 'home', 'http://localhost:4200/home');它确实会向浏览器历史记录中添加一个新的状态,但当我单击浏览器的后退按钮时,什么也不会发生,即我仍然在同一页面中,但只有新添加的状态会从浏览器历史记录中删除。
发布于 2019-04-05 19:44:58
我已经在你的另一个问题中回答了这个问题:https://stackoverflow.com/a/55511843/11289490
当你调用history.pushState时,它只会用你想要的url将一个页面添加到历史记录中,但不会加载该网页,也不会检查该网页是否存在。
如果你想在返回时加载主页,你可以这样做:
let locat = location.href;
if (!!window.location.pathname && window.location.pathname !== '/') {
history.replaceState(null,null,location.origin);
history.pushState(null, null,locat);
}它会添加一个基于url的历史记录,然后在历史记录中添加完整的url。
https://stackoverflow.com/questions/55494629
复制相似问题