前端路由-JS实现
/* * 路由源于服务器端的概念 * 路由在服务端是URL到控制器(处理函数的映射) * 路由在前端是URL到UI的映射 */ ;(function () { function Router(){ this.historyStack = []; // 记录跳转历史 this.registerRouter = []; // 记录已注册的路由 this.defalutRouter = { path: '/', content: 'default page!' }; // 路由匹配失败时的跳转地址 } // 路由启用 Router.prototype.init = function() { }; // 绑定 hashchange 事件的回调函数 Router.prototype._bindEvents = function(){ } // 路由注册方法 Router.prototype.add = function(path, content){ } // 判断新增路由是否存在 Router.prototype.isHasRouter = function(path){ } // 路由不存在时的默认路由 Router.prototype.default = function(path, content){ } // 路由跳转方法 Router.prototype.go = function(path){ } // 渲染对应路由信息到页面 Router.prototype.render = function(content){ } var router = new Router() window.$router = router; // 将接口暴露到全局 })()