history实现前端路由
来源:互联网 发布:淘宝联盟身份认证 编辑:程序博客网 时间:2024/04/28 08:49
这里要注意有一个坑,就是history模式天然是要服务器支持的,也就是说至少也需要起一个服务器。不然“///”这种样式的路径是不会识别的。
<!DOCTYPE html><html><head> <title></title></head><body><a href="/about">about</a><a href="/">home</a><a href="/test">test</a><a href="/about/test">about_test</a><div id="app"></div><script type="text/javascript">function Router(routeOption,id){ this.routes=routeOption; this.currentRoute = ''; this.dom = id ? document.getElementById(id) : null; } Router.prototype = { init(){ window.addEventListener('popstate', () => { this.currentRoute = window.location.pathname; this.routes[this.currentRoute] ? this.dom ? this.dom.innerText = this.routes[this.currentRoute] : null : null; }) }, _updateState(route){ window.history.pushState( null, this.routes[route], route ) }, route(route){ this._updateState(route); } } let router = new Router({ '/': 'Home', '/about': 'About', '/about/test': 'About_test' },'app'); router.init(); document.querySelectorAll('a').forEach(function(item,index){ item.addEventListener('click',function(e){ e.preventDefault(); let route = item.getAttribute('href'); router.route(route); }) })</script></body></html>
阅读全文
0 0
- history实现前端路由
- 利用history.pushState实现前端路由
- 前端路由实现
- 前端路由相关实现
- 前端路由实现原理
- Javascript实现前端简单路由
- hash模式实现前端路由
- 前端路由的实现原理
- Director JS 实现前端路由
- history.pushState()实现不跳转,改变路由异步刷新
- 用director.js实现的前端路由
- 前端路由的不同方法实现
- 前端路由的两种实现原理
- 前端路由的两种实现原理
- 前端路由
- 前端路由
- 【前端路由】
- 前端路由
- 斐波那契数列算法c语言实现
- CRC校验
- 不同算法策略特点小结
- vue-cli快速搭建vue项目并上传github
- Android启动过程深入解析
- history实现前端路由
- Android AnimationDrawable资源 set[translate,alpha,scale,rotate]
- JavaScript实现简单的双向数据绑定(Ember、Angular、Vue)
- centos下更换默认的python版本
- Java+maven+selenium+testng+jenkins自动化环境搭建(补充)
- linux进程
- Spring Boot从原理到实战
- Java数据结构与算法---队列
- Android 设计模式相关面试题