Angular: take user to home on clicking browser back button [duplicate]

為{幸葍}努か 提交于 2019-12-02 03:33:53

You can use history.pushState(null, null, 'https://twitter.com/hello'); and pass as third parameter the home link of your app. In this case pressing back will move the user to the Home page.

Have a look here; window.history API

Or: css-tricks

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);
    }

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.

Example here:

https://stackblitz.com/edit/angular-stack-home-redirect-history?file=src/app/app.component.ts

Try it out here: https://angular-stack-home-redirect-history.stackblitz.io/bye

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!