Get the path change url parameters js

When we jump in the router for routing use or react vue, in order to maintain a consistent state address bar menu, we can use the change window.onhashchange captured behind #

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

 

Guess you like

Origin www.cnblogs.com/zhixi/p/10929115.html