SOURCE

console 命令行工具 X clear

                    
>
console
window.addEventListener('DOMContentLoaded', e => {
    location.hash = `/${location.hash}`
    console.log(location.hash)
})

window.addEventListener('hashchange', e => {
    const opt = document.querySelector('#opt')
    opt.value = location.href
})

window.addEventListener('load', e => {
    const btns = document.querySelectorAll('.btn')
    btns.forEach(item => {
        item.addEventListener('click', e => {
            const hash = item.dataset.hash
            location.hash = `/${hash}`
        })
    })
})
<div>
    <div>当前路由:<output id="opt"></output></div>
    <button class="btn" data-hash="/">home</button>
    <button class="btn" data-hash="/foo">foo</button>
    <button class="btn" data-hash="/bar">bar</button>
</div>