ブラウザのルーティングを傍受する方法---履歴を傍受する

// 拦截history的方法,因为pushState和replaceState方法并不会触发onpopstate事件,
      const originalPushState = window.history.pushState;
      const originalReplaceState = window.history.replaceState;
      window.history.pushState = function (state, title, url) {
         originalPushState.apply(this, arguments);
         console.log("拦截1",location.pathname)

      };
      window.history.replaceState = function (state, title, url) {
        originalReplaceState.apply(this, arguments);
        console.log("拦截2",location.pathname)
      };

      //回退拦截
      window.addEventListener('popstate',function () {
        console.log("拦截2",location.pathname)
      })

 

80件の元の記事を公開 Likes5 訪問40,000+

おすすめ

転載: blog.csdn.net/qq_28473733/article/details/103176098