vue路由实现原理

5 篇文章 0 订阅

答:前端路由实现原理主要通过以下两种技术实现的
第一种:利用H5的history API实现
主要通过history.pushState 和 history.replaceState来实现,不同之处在于,pushState会增加一条新的历史记录,而replaceState则会替换当前的历史记录[发布项目时,需要配置下apache]

第二种:利用url的hash实现
我们经常在 url 中看到 #,这个 # 有两种情况,一个是我们所谓的锚点,路由里的 # 不叫锚点,我们称之为 hash,我们说的就是hash,主要利用监听哈希值的变化来触发事件 —— hashchange 事件来做页面局部更新
hash模式实现的代码如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>hash模式</title>
    <style>
      #about {
        display: none;
      }
      #info {
        display: none;
      }
    </style>
  </head>
  <body>
    <p>
      <a href="#/home">首页</a>|<a href="#/about">关于</a>|<a href="#/info"
        >信息</a
      >
    </p>
    <div id="home">
      <h1>首页</h1>
    </div>
    <div id="about">
      <h1>关于</h1>
    </div>
    <div id="info">
      <h1>信息</h1>
    </div>
    <div class="qqq"></div>
    <script>
      const router = [
        { path: "#/home", component: home },
        { path: "#/about", component: about },
        { path: "#/info", component: info },
      ]
      let currentView = router[0] // 默认显示第一个页面

      window.onhashchange = function (e) {
        for (let i = 0; i < router.length; i++) {
          if (location.hash == router[i].path) {
            // 如果URL匹配到
            currentView.component.style.display = "none" // 关闭默认显示的页面
            router[i].component.style.display = "block" // 显示匹配到的页面
            currentView = router[i] // 更改默认显示的页面为当前页面
            break //匹配到之后跳出循环,提高性能
          }
        }
      }
    </script>
  </body>
</html>

history模式实现的代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #about {
        display: none;
      }
      #info {
        display: none;
      }
    </style>
  </head>
  <body>
    <p><a id="home1">首页</a>|<a id="about1">关于</a>|<a id="info1">信息</a></p>
    <div id="home">
      <h1>首页</h1>
    </div>
    <div id="about">
      <h1>关于</h1>
    </div>
    <div id="info">
      <h1>信息</h1>
    </div>
    <script>
      let cur = home
      home1.onclick = function () {
        cur.style.display = "none"
        cur = home
        home.style.display = "block"
        history.pushState({ page: 1 }, "title 1", "home")
      }
      about1.onclick = function () {
        cur.style.display = "none"
        cur = about
        about.style.display = "block"
        about.style.display = "block"
        history.pushState({ page: 1 }, "title 1", "about")
      }
      info1.onclick = function () {
        cur.style.display = "none"
        cur = info
        info.style.display = "block"
        info.style.display = "block"
        history.replaceState({ page: 1 }, "title 1", "info")
      }
    </script>
  </body>
</html>

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白嫖leader

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值