阻止用户点击浏览器后退按钮Angular2

时间:2018-01-09 11:38:59

标签: browser navigation angular2-routing

我正在使用带有Typescript的Angular 2 JS。我有一个要求,我不想通过浏览器后退按钮点击/导航到上一页。

所以我们如何禁用或阻止用户通过浏览器点击或移动到上一页。

我尝试了一些可通过互联网获得的示例,但这些示例无效。喜欢 -

this.router.navigate(['/view'], { replaceUrl: true });

this.router.navigate(['/view'], { skipLocationChange: true });

1 个答案:

答案 0 :(得分:0)

在页面加载时使用此功能,它将阻止用户从其放置的页面导航回来。

将代码放在angular

的index.html页面中
 <script type = "text/javascript">
    function changeHashOnLoad() {
        window.location.href += "#";
        setTimeout("changeHashAgain()", "50");
    }

    function changeHashAgain() 
    {          
        window.location.href += "1";
    }

    var storedHash = window.location.hash;
    window.setInterval(function () {
        if (window.location.hash != storedHash) {
            window.location.hash = storedHash;
        }
    }, 50);

    </script>
相关问题