当我们在使用react或vue的router作路由跳转时,为了保持菜单与地址栏状态一致,我们可以使用window.onhashchange捕获#后面的变化

    window.onhashchange = (hash)=> {
        var newHash = this.getHashKeyByUrl(hash.newURL);
        if(newHash == this.getHashKeyByUrl(hash.oldURL)){
            return;
        }
        // this.setState({curSelectedMenuKey:[newHash]});
        console.log("url参数-->",newHash)
    };

    function getHashKeyByUrl(url){
        var reg = new RegExp(/#\/(.*)?/);
        var matches =url.match(reg);
        if(matches && matches.length>=2 && matches[1]!=undefined){
            return matches[1];
        }else{
            return null;
        }
    }

 

相关文章:

  • 2021-10-17
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
  • 2021-09-15
  • 2021-11-20
  • 2021-11-20
  • 2021-11-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2021-11-20
  • 2022-03-10
  • 2022-02-28
相关资源
相似解决方案