aiyoudiao aiyoudiao
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档

码二

扫微信二维码,认识一下码二吧😉。
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档
  • JavaScript

    • 实现一个vdom
    • 前端路由监听
      • 前言
      • 哈希改变
      • history改变
      • 总结
    • 定制自己的代码片段
    • 写一个下载文件功能
    • 正则表达式
    • JS性能优化
    • JS性能优化进阶
  • vue

  • react

  • 低代码

  • 读书会

  • 线性代数

  • docker

  • auto

  • 杂记

  • 笔记
  • JavaScript
aiyoudiao
2022-04-15

前端路由监听

# 前言

前端路由监听是通过两个事件来做到的,分别是 哈希改变 和 history改变

# 哈希改变

hashchange

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>

  <div id="app">
    <a href="#/home">首页</a>
    <a href="#/about">关于</a>

    <div class="router-view"></div>
  </div>
  
  <script>
    // 获取router-view的DOM
    const routerView = document.querySelector(".router-view");

    // 监听URL的改变
    window.addEventListener("hashchange", () => {
      switch (location.hash) {
        case "#/home":
          routerView.innerHTML = "首页";
          break;
        case "#/about":
          routerView.innerHTML = "关于";
          break;
        default:
          routerView.innerHTML = "404";
      }
    })
  </script>
</body>
</html>

# history改变

popstate

pushState、replaceState

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>

  <div id="app">
    <a href="/home">首页</a>
    <a href="/about">关于</a>

    <div class="router-view"></div>
  </div>

  <script>
    // 1.获取router-view的DOM
    const routerView = document.querySelector(".router-view");

    // 获取所有的a元素, 自己来监听a元素的改变
    const aList = document.querySelectorAll("a");
    for (let el of aList) {
      el.addEventListener("click", event => {
        event.preventDefault();
        const href = el.getAttribute("href");
        // data, title, url
        history.pushState({}, "", href);
        urlChange();
      })
    }

    // 执行返回操作时, 依旧来到urlChange
    window.addEventListener('popstate',urlChange);

    // 监听URL的改变
    function urlChange() {
      switch (location.pathname) {
        case "/home":
          routerView.innerHTML = "首页";
          break;
        case "/about":
          routerView.innerHTML = "关于";
          break;
        default:
          routerView.innerHTML = "404";
      }
    }

  </script>

</body>
</html>

# 总结

实现前端路由的监听是一件很容易的事情,毕竟官方提供了api。

通过监听hash和判断location的hash 或者 监听所有跳转标签的单击、以及history的state变更还有判断location的pathname,这些都可以实现前端路由的监听的效果。

#JavaScript
上次更新时间: 10年18月2023日 01时57分53秒
实现一个vdom
定制自己的代码片段

← 实现一个vdom 定制自己的代码片段 →

最近更新
01
01.数据结构导论一览.md
10-16
02
30.2023年06月04日.md
06-04
03
08.与测量相关.md
05-06
更多文章>
Theme by Vdoing | Copyright © 2017-2023 aiyoudiao 码二 备案号: 鄂ICP备2022002654号-1