This theme is from Cn|

guangzan

园龄:6年5个月粉丝:676关注:333

2019-11-14 19:50阅读: 254评论: 0推荐: 0

vue SPA设计 history hash

<body>
  <h3>Histort api</h3>
  <a class="api a">a,html</a>
  <a class="api b">b,html</a>

  <script>
    //注册路由
    document.querySelectorAll('.api').forEach(item => {
      item.addEventListener('click', (e) => {
        e.preventDefault();
        let link = item.textContent;
        window.history.pushState({name: 'api'},link,link)
      }, false)
    });

    //监听路由
    window.addEventListener('popstate', (e) => {
      console.log({
        location: location.href,
        state: e.state
      });
    }, false)
  </script>

  <p>------------------</p>

  <h3>Hash</h3>
  <a class="hash a">#a.html</a>
  <a class="hash b">#b.html</a>

  <script>
    //注册路由
    document.querySelectorAll('.hash').forEach(item => {
      item.addEventListener('click', (e) => {
        e.preventDefault();
        let link = item.textContent;
        location.hash=link;
      }, false)
    });

    //监听路由
    window.addEventListener('hashchange', (e) => {
      console.log({
        location: location.href,
        hash: location.hash
      });
    }, false)
  </script> 
</body>

本文作者:guangzan

本文链接:https://www.cnblogs.com/guangzan/p/11861091.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   guangzan  阅读(254)  评论(0编辑  收藏  举报
评论
收藏
关注
推荐
深色
回顶
收起
点击右上角即可分享
微信分享提示