应用hash或history完成单页面路由

[toc]

在chrome(版本 70.0.3538.110)测试一般

编写触及:css, html,js, node(koa)

在线演示codepen

html代码

 <div class="hash">
        <div class="title">hash 路由</div>
        <a href="#/one">hash 1</a> <a href="#/two">hash 2</a> <a href="#/three">hash 3</a> <a onclick="hashRoute.skip('other')">other</a>
        <div id="hashContent"></div>
    </div>
    <div class="history">
        <div class="title">history 路由</div>
        <div>
            <button onclick="historyRoute.skip('pushStateOne')">history.pushState(1)</button>
            <button onclick="historyRoute.skip('pushStateTwo')">history.pushState(2)</button>
            <button onclick="historyRoute.skip('pushStateThree')">history.pushState(3)</button>
            <button onclick="historyRoute.skip('pushStateTwo')">history.replaceState(pushStateTwo)</button>
            <button onclick="historyRoute.skip('go')">history.go(2)</button>
            <button onclick="historyRoute.skip('forward')">history.forward()</button>
            <button onclick="historyRoute.skip('back')">history.back()</button>
        </div>
        <div id="historyContent"></div>
    </div>

css代码

.hash a {
    display: inline-block;
    padding: 5px 8px;
    margin: 10px 10px 10px 0;
    font-size: 15px;
    text-decoration: none;
    border: 0;
    cursor: pointer;
    color: #fff;
    background-color: rgb(17, 130, 236);
}
.title{
    margin: 10px 0;
    padding: 5px 8px;
    border-left: rgb(168, 168, 168) solid 2px;
    background-color: rgb(230, 230, 230);
}
.hash div:last-child{
    padding: 6px;
    min-height: 100px;
    background-color: rgb(243, 243, 243);
}

.history{
    margin: 10px 0;
}
.history button {
    padding: 8px 10px;
    border: 0;
    color: #fff;
    background-color: rgb(250, 144, 44);
}
.history div:last-child{
    margin-top: 10px;
    padding: 6px;
    min-height: 100px;
    background-color: rgb(243, 243, 243);
}

JavaScript代码

hash体式格局

class HashRoute {
    setRoute() {
        const commandObj = {
            one: 'page one',
            two: 'page two',
            three: 'page three'
        }
        const hashRoute = location.hash ? location.hash.slice(2) : 'one'
        let re = commandObj[hashRoute]

        document.getElementById('hashContent').innerHTML =  re ? re : 'page not find'
    }

    skip(path) {
        window.location.hash= `#/${path}`
    }

    init() {
        window.addEventListener('DOMContentLoaded', this.setRoute)

        // 1.直接变动浏览器地点,在最背面增添或转变#hash; 
        // 2.经由过程转变location.href 或 location.hash的值; 
        // 3.经由过程触发点击带锚点的链接; 
        // 4.浏览器行进退却能够致使hash的变化,条件是两个网页地点中的hash值差别
        window.addEventListener('hashchange', this.setRoute)
    }
}

const hashRoute = new HashRoute()

hashRoute.init()

history 体式格局

浏览器端代码

// 服务端有用
class HistoryRoute {
    constructor() {
        this.currentPath = ''
    }

    renderView(component) {
        const route = {
            pushStateOne: 'route pushState one',
            pushStateTwo: 'route pushState two',
            pushStateThree: 'route pushState three',
            replaceState: 'route replaceState',
            go: 'route go',
            forward: 'route forward',
            back: 'route back',
            notFind: 'not find',
        }
        document.getElementById('historyContent').innerHTML = route[component]
    }

    // 这里一切触及的跳转都用js体式格局,不采纳a标签(采纳a标签请设置阻拦)
    skip(path) {
        const commandObj = {
            pushStateOne: () => {
                history.pushState({ path }, path,path)
                this.renderView(path)
            },
            pushStateTwo: () => {
                history.pushState({ path }, path, path)
                this.renderView(path)
            },
            pushStateThree: () => {
                history.pushState({ path }, path, path)
                this.renderView(path)
            },
            replaceState: () => {
                // 是用来修正当前的history实体而不是建立一个新的,比方连转递次为1,2,3,1实行replaceState(2),再实行back(),返回1,而不是3
                history.replaceState({ path }, path, path)
                this.renderView(path)
            },
            go: () => {
                history.go(2)
                this.renderView('go')
            },
            forward: () => {
                history.forward()
                this.renderView('forward')
            },
            back: () => {
                history.back()
            },

        }

        this.currentPath = path;
        commandObj[path]()
    }

    init() {
        window.addEventListener('DOMContentLoaded', () => {
            // 针对F5革新题目:
            // 1.能够运用?背面跟参数情势
            // 2.一致进口应用疏忽地点体式格局(后端设置 /page/:path 疏忽page后跟的一切地点,经由过程前端去要求page后的对应路由数据,以下)

            const path = location.href.split('/page/') 
            this.skip(path[1])
        })

        // 挪用history.pushState()或history.replaceState()不会触发popstate。
        // 只要在用户点击行进回退按钮,(或history.back(),forward,go)
        window.addEventListener('popstate', (event) => {
            console.log('popstate', this.currentPath, event.state);
            const { state } = event
            if (state && state.path) {
                this.renderView(state.path)
            } else {
                this.renderView('404')
            }
        })
    }
}

const historyRoute = new HistoryRoute()

historyRoute.init();

服务器端

// 连系前端,能够用以下体式格局处置惩罚
router.get('/page/:path', (ctx, next) => {
    ctx.response.type = 'html';
    // 此处的singlePageRoute.html为单页面html容器,即安排本文中的一切代码文件
    ctx.response.body = fs.createReadStream('./dist/public/files/singlePageRoute.html');
    return next();
})

迎接交换
Github

    原文作者:Hewitt
    原文地址: https://segmentfault.com/a/1190000017240640
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞