vue路由实现原理

答:前端路由实现原理主要通过以下两种技术实现的
第一种:利用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>

你可能感兴趣的:(vue,笔记,vue.js,javascript,前端)