hash模式 不会刷新页面
window.onhashchange = (e) => {
console.log('老url', e.oldURL);
console.log('新url', e.newURL);
console.log('hash', location.hash)
}
window.addEventListener('DOMContentLoaded', () => {
console.log(location.hash)
})
location.href = '#/user';
history模式 服务端需要配置无论什么页面都要返回index.html
window.addEventListener('DOMContentLoaded', () => {
console.log('path:',location.pathname)
})
const state = { name: 'user' }
history.pushState(state, '', 'user');
console.log('切换路由到了user')
window.onpopstate = (e) => {
console.log('onpopstate', e.state, location.pathname)
}