简体   繁体   English

关闭侧面菜单后单击

[英]Close side menu on back click

Im building an Angular application and having some troubles with closing the menu on popstate. 我正在构建一个Angular应用程序,并在popstate上关闭菜单遇到了一些麻烦。 when the side menu is open and the user clicked on the back button on his mobile device i want the menu to close the menu, for that im using pop state like that 当侧面菜单打开并且用户单击其移动设备上的后退按钮时,我希望菜单关闭菜单,为此,我使用诸如此类的弹出状态

@HostListener('window:popstate', ['$event'])
  onPopState(event) {
    if (this.isMenuOpened) {
      this.toggleSidebar();
    }
      return ;
  }

The problem Im having is that the back action also works, so the menu is close but the back action is also happens. Im的问题是后退动作也起作用,因此菜单关闭,但后退动作也发生。 i cant use history.go(1) because it makes the page load again and i don't want this kind of behaviour. 我不能使用history.go(1)因为它会使页面再次加载,并且我不希望这种行为。

did you have any ideas ? 你有什么主意吗?

Popstate is not cancellable as per the specs . 根据规范, Popstate无法取消。 However, you can try to do this 但是,您可以尝试执行此操作

@HostListener('window:popstate', ['$event'])
onPopState(event) {
    if (this.isMenuOpened) {
        event.preventDefault();
        history.go(1); // re-changes the url to what it should be
        this.toggleSidebar();
    }
    return ;
}

try this - 尝试这个 -

 $(window).on( "popstate", function(event){
          if( !event.originalEvent.state ){
          history.pushState( "nohb", null, "" );
          return;
          }
      });

Instead of history.go(1) use window.history.forward() 代替history.go(1)使用window.history.forward()

window.history.forward() this function in the first page uses the history of the browser and forces it to navigate forward instead of going to the previous page. window.history.forward()此函数在第一页中使用浏览器的历史记录,并强制其向前浏览而不是转到上一页。 Therefore, every time the user clicks the back button, it will result in the Browser navigating or pushing the user forward and showing the same page. 因此,每次用户单击“后退”按钮,都会导致浏览器向前导航或推动用户并显示同一页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM