美文网首页
Vue路由「十五」-- vue-router的路由模式***

Vue路由「十五」-- vue-router的路由模式***

作者: loushumei | 来源:发表于2021-01-03 23:05 被阅读0次

    vue-router的路由模式有 hash模式 和 history模式,默认使用 hash 模式
    hash模式:使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载。例如 http://yoursite.com/#/user/id
    history 模式:URL 就像正常的 url,例如 http://yoursite.com/user/id

    前置知识:网页url组成部分

    https://127.0.0.1:8082/lesson/419.html?a=100&b=20#mid=33876
    location.protocol //https: 协议
    location.hostname //127.0.0.1 ip地址/域名
    location.host //127.0.0.1:8082
    location.port //8082 端口
    location.pathname // /lesson/419.html
    location.search // ?a=100&b=20
    location.hash // #mid=33876

    1.hash

    hash的特点
    • hash 变化会触发网页跳转,即浏览器的前进后退
    • hash 变化不会刷新页面,SPA(single page web application)必备
    • hash 永远不会提交到server端
    核心API

    onhashchange 事件在当前 URL 的锚部分(以 '#' 号为开始) 发生改变时触发,hash变化包括:

    • 手动修改的url
    • 手动修改url的hash
    • 浏览器的前进和后退
    vue实现hash路由的原理

    利用 hash 变化不会刷新页面的特点;
    然后用 window.onhashchange 监听 hash 的变化来做到触发对应时间加载对应资源来重新绘制页面。

    <p>hash test</p>
    <button id="btn1">修改 hash</button>
    <script>
        window.onhashchange = (event) => {
            console.log('old url', event.oldURL)
            console.log('new url', event.newURL)
            console.log('hash', location.hash)
        }
        // 首页初次加载,获取hash
        document.addEventListener('DOMContentLoaded', () => {
            console.log('hash', location.hash)
        })
        // js修改url
        document.getElementById('btn1').addEventListener('click', () => {
            location.href = "#/user"
        })
    </script>
    

    2.h5 history模式

    用url规范的路由,但跳转时不刷新页面

    核心API :

    history.pushState() history.replaceState() : HTML5新接口,可以改变网址(存在跨域限制)而不刷新页面
    window.onpopstate: 在点击后退、前进按钮(或调用history.back()、history.forward()、history.go()方法)时触发。前提是不能真的发生了页面跳转,而是在由history.pushState()或者history.replaceState()形成的历史节点中前进后退

    vue实现hash路由的原理

    利用 history.pushState() history.replaceState() 不会刷新页面的特点;
    然后用 window.onpopstate 监听路由的变化来做到触发对应时间加载对应资源来重新绘制页面。

    <p>H5 history</p>
    <button id="btn1">修改</button>
    <script>
        // 首页初次加载,获取path
        document.addEventListener('DOMContentLoaded', () => {
            console.log(location.pathname)
        })
        // 打开一个新路由
        // 【注意】用pushState方式,浏览器不会刷新页面
        // history.pushState() 方法向浏览器历史添加了一个状态。
        // pushSteate() 接受三个参数:state 对象, title (目前被忽略了),URL
        document.getElementById('btn1').addEventListener('click', () => {
            const state = {
                name: 'page1'
            }
            console.log('切换路由到', 'page1')
            history.pushState(state, '', 'page1') // pushState ***
        })
        //监听浏览器前进后退 onpopstate
        window.onpopstate = (event) => { //onpopstate *** 
            console.log('onpopstate', event.state, location.pathname)
        }
    </script>
    
    后台配置支持:

    因为我们的应用是个单页客户端应用,如果后台没有正确的配置,当用户在浏览器直接访问 http://oursite.com/user/id 就会返回 404,这就不好看了。
    服务端增加一个覆盖所有情况的候选资源:如果 URL 匹配不到任何静态资源,则应该返回同一个 index.html 页面,这个页面就是你 app 依赖的页面。

    前端配置:

    前端需要给个警告,因为这么做以后,你的服务器就不再返回 404 错误页面,因为对于所有路径都会返回 index.html 文件。为了避免这种情况,你应该在 Vue 应用里面覆盖所有的路由情况,然后在给出一个 404 页面。

    const router = new VueRouter({
      mode: 'history',
      routes: [
        { path: '*', component: NotFoundComponent }
      ]
    })
    

    3 .总结

    区别:

    • hash通过window.onhashchange监听hash变化
    • H5 history通过history.pushState()向浏览器历史添加了一个记录,通过onpopstate监听浏览器前进后退
    • H5 history需要后端支持

    两者选择

    • To B系统推荐使用hash,简单易用,对url规范不敏感
    • To C系统,可以考虑H5 history,但需要服务端支持

    相关文章

      网友评论

          本文标题:Vue路由「十五」-- vue-router的路由模式***

          本文链接:https://www.haomeiwen.com/subject/kotmoktx.html