当前位置: 首页 > article >正文

html 单页面路由模式hash和history

文章目录

    • Hash 模式
    • H5的history模式

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>Document</title>
</head>
<body>
  <nav class="nav-box">
    <a href="#/">首页</a>
    <a href="#/about">关于</a>
    <a href="#/product">产品</a>
  </nav>

  <div id="app"></div>

  <script>
    const app = document.querySelector('#app')
    const navBox = document.querySelector('.nav-box')

    const routes = [
      {
        path: '/',
        component: '组件-首页home' // 应是组件
      },
      {
        path: '/about',
        component: '组件-关于我们'
      },
      {
        path: '/product',
        component: '组件-产品列表'
      }
    ]
    
    const routeMatch = () => {
      const hash = location.hash.substring(1) // 去掉hash的 #
      let text = ''
      routes.forEach(it => {
        if(it.path === hash) text = it.component
      })
      app.innerHTML = text
    }

    // 首次渲染 /
    location.hash = '/'
    routeMatch()
    // 监听 hash 变化
    window.onhashchange = routeMatch
  </script>
</body>
</html>

H5的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>
</head>
<body>
  <nav class="nav-box">
    <a href="/">首页</a>
    <a href="/about">关于</a>
    <a href="/product">产品</a>
  </nav>

  <div id="app"></div>

  <script>
    const app = document.querySelector('#app')
    const navBox = document.querySelector('.nav-box')

    const routes = [
      {
        path: '/',
        component: '组件-首页home' // 应是组件
      },
      {
        path: '/about',
        component: '组件-关于我们'
      },
      {
        path: '/product',
        component: '组件-产品列表'
      }
    ]
    
    // 事件委托,点击事件
    navBox.onclick = (event) => {
      if(event.target.tagName === 'A') {
        event.preventDefault() // 阻止默认事件,跳转并刷新
        // Failed to execute 'pushState' on 'History': A history state object with URL ' cannot be created in a document with origin 'null' and URL '
        history.pushState({}, '', event.target.href)
        routeMatch()
      }
    }

    const routeMatch = () => {
      const pathname = location.pathname
      let text = ''
      routes.forEach(it => {
        if(it.path === pathname) text = it.component
      })
      app.innerHTML = text
    }

    // 首次渲染 /
    history.pushState(null, '', '/')
    routeMatch()

    // 监听popstate地址变化事件; 执行go/forward/back等方法可以触发
    window.onpopstate = routeMatch

  </script>
</body>
</html>

http://www.kler.cn/news/294368.html

相关文章:

  • Shell脚本基本语法(Linux篇)
  • MapSet之二叉搜索树
  • 1-7 掩膜的运用 opencv树莓派4B 入门系列笔记
  • 力扣239题详解:滑动窗口最大值的多种解法与模拟面试问答
  • GNN会议期刊汇总(人工智能、机器学习、深度学习、数据挖掘)
  • kubernetes--配置与存储(ConfigMap、加密数据配置Secret、SubPath、热更新、Volumes、NFS挂载、PV与PVC)
  • C#基础(5)交错数组*
  • 【Rust光年纪】Rust 机器人学库全景:功能、安装与API概览
  • 多线程篇(阻塞队列- BlockingQueue)(持续更新迭代)
  • 不同vlan之间的通信方法
  • 微信小程序仿微信聊天界面
  • 【spring】does not have member field ‘com.sun.tools.javac.tree.JCTree qualid
  • 【网络安全】密码学概述
  • 『功能项目』更换URP场景【32】
  • 【BUUCTF】HardSQL
  • 交换两个变量数值的3种方法
  • 创建Hive表后,查看表结构发现中文注释乱码
  • 【C++模版初阶】——我与C++的不解之缘(七)
  • Maven使用指南的笔记
  • 笔试强训,[NOIP2002普及组]过河卒牛客.游游的水果大礼包牛客.买卖股票的最好时机(二)二叉树非递归前序遍历
  • uniapp使用uni-popup做底部弹出选项(vue3)
  • R语言中rds 文件是什么,都保存了什么数据,详解
  • 宠物浮毛对身体危害竟这么大?再不预防就来不及了
  • Selenium4.0详细介绍
  • 龙芯+FreeRTOS+LVGL实战笔记(新)——05部署主按钮
  • 从零开始,认识游戏设计师(4)体验源于设计师②
  • 数据结构----链表
  • C# 特性与属性的区别
  • iOS 中,用户点击一个按钮到响应的全部流程
  • 【网络安全】服务基础第二阶段——第二节:Linux系统管理基础----Linux统计,高阶命令