更新时间:2023-12-03 10:29:40
他们在该页面中的操作有点棘手。
诀窍是使用 history.pushState
创建新的历史记录,然后订阅该位置的pop事件以将网页更改为首页。
What they did in that page is a bit tricky.
The trick is creating a new history with history.pushState
and then subscribe to the pop event of the location to change the webpage to the home .
我建议做一些不同且更容易的事情,该代码只需要以下几行:
I suggest to do something a little different and easier, that code only needs this lines:
let locat = location.href;
if (!!window.location.pathname && window.location.pathname !== '/') {
history.replaceState(null,null,location.origin);
history.pushState(null, null,locat);
}
我们在这里所做的是将URL更改为您要使用 history.replaceState(null,null,location.origin); ,然后使用 history.pushState(null,null,locat)在历史记录中新建一个条目;
并带有第一个网址。
What we do here it's change the url to the home you want with history.replaceState(null,null,location.origin);
and then make a new entry in the history with history.pushState(null, null,locat);
witht he first url.
此处的示例:
https:/ /stackblitz.com/edit/angular-stack-home-redirect-history?file=src/app/app.component.ts
尝试一下此处:
https:// angular-stack-home-redirect- history.stackblitz.io/bye