html 单页面路由模式hash和history

打印 上一主题 下一主题

主题 975|帖子 975|积分 2940

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Hash 模式

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.   <title>Document</title>
  8. </head>
  9. <body>
  10.   <nav class="nav-box">
  11.     <a href="#/">首页</a>
  12.     <a href="#/about">关于</a>
  13.     <a href="#/product">产品</a>
  14.   </nav>
  15.   <div id="app"></div>
  16.   <script>
  17.     const app = document.querySelector('#app')
  18.     const navBox = document.querySelector('.nav-box')
  19.     const routes = [
  20.       {
  21.         path: '/',
  22.         component: '组件-首页home' // 应是组件
  23.       },
  24.       {
  25.         path: '/about',
  26.         component: '组件-关于我们'
  27.       },
  28.       {
  29.         path: '/product',
  30.         component: '组件-产品列表'
  31.       }
  32.     ]
  33.    
  34.     const routeMatch = () => {
  35.       const hash = location.hash.substring(1) // 去掉hash的 #
  36.       let text = ''
  37.       routes.forEach(it => {
  38.         if(it.path === hash) text = it.component
  39.       })
  40.       app.innerHTML = text
  41.     }
  42.     // 首次渲染 /
  43.     location.hash = '/'
  44.     routeMatch()
  45.     // 监听 hash 变化
  46.     window.onhashchange = routeMatch
  47.   </script>
  48. </body>
  49. </html>
复制代码
H5的history模式

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.   <title>Document</title>
  8. </head>
  9. <body>
  10.   <nav class="nav-box">
  11.     <a href="/">首页</a>
  12.     <a href="/about">关于</a>
  13.     <a href="/product">产品</a>
  14.   </nav>
  15.   <div id="app"></div>
  16.   <script>
  17.     const app = document.querySelector('#app')
  18.     const navBox = document.querySelector('.nav-box')
  19.     const routes = [
  20.       {
  21.         path: '/',
  22.         component: '组件-首页home' // 应是组件
  23.       },
  24.       {
  25.         path: '/about',
  26.         component: '组件-关于我们'
  27.       },
  28.       {
  29.         path: '/product',
  30.         component: '组件-产品列表'
  31.       }
  32.     ]
  33.    
  34.     // 事件委托,点击事件
  35.     navBox.onclick = (event) => {
  36.       if(event.target.tagName === 'A') {
  37.         event.preventDefault() // 阻止默认事件,跳转并刷新
  38.         // Failed to execute 'pushState' on 'History': A history state object with URL ' cannot be created in a document with origin 'null' and URL '
  39.         history.pushState({}, '', event.target.href)
  40.         routeMatch()
  41.       }
  42.     }
  43.     const routeMatch = () => {
  44.       const pathname = location.pathname
  45.       let text = ''
  46.       routes.forEach(it => {
  47.         if(it.path === pathname) text = it.component
  48.       })
  49.       app.innerHTML = text
  50.     }
  51.     // 首次渲染 /
  52.     history.pushState(null, '', '/')
  53.     routeMatch()
  54.     // 监听popstate地址变化事件; 执行go/forward/back等方法可以触发
  55.     window.onpopstate = routeMatch
  56.   </script>
  57. </body>
  58. </html>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大连全瓷种植牙齿制作中心

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表