您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

JavaScript写Web路由代码分享

2024/3/27 11:51:39发布18次查看
本文主要和大家分享javascript 写web路由代码分享,单页面现在是一种很流行的应用程序,可以说是到处可见。而单页面中有一个非常重要的概念,那就是路由机制,也就是说单页面应用程序要能正常的运行,就意味着需要一个可靠的路由机制。接下来看看@krasimirtsonev是怎么实现的。
目标这个路由将会具备:
代码少于100行
支持hash类型的url,比如http://site.com#products/list
使用html history api
提供易于使用的api
不自动运行,只是需要的改变的时候才运行
实现思路这个示例设计的是只有一个路由器实例。当然这可能不是一个很好的选择,那是因为我们在一个应用程序中,特别是在一个复杂的运用程序中,需要多个路由器才能实现。如果我们实现单例的模式,就不需要将路由器从对象传递到对象。所以我们先创建一个对象router:
let router = { routes: [], mode: null, root: '/'}
router对象中设置了三个属性:
routes:用于保持当前注册的路由
mode:可以使用hash或者history模式
root:应用程序的根url路径,只有当我们使用了history.pushstate()时才需要它
接下来需要创建一个路由器的方法,有两件事情需要处理,可以在一个函数内来处理:
let router = { routes: [], mode: null, root: '/', config: function (options) { this.mode = options && options.mode && (options.mode = 'history') && !!(history.pushstate) ? 'history' : 'hash'; this.root = options && options.root ? '/' + this.clearslashes(options.root) + '/' : '/' return this } }
只有当我们需要和只有在支持pushstate的情况下,mode值等于history,否则将会会使用url中的hash。root默认设置为一个/。
路由器中重要的一部分是获取当前url,因为它会告诉我们此刻在哪里。获取当前url有两种模式,所以这里使用if语句来处理:
getfragment: function () { let fragment = ''; if (this.mode === 'history') { fragment = this.clearslashes(decodeuri(location.pathname + location.search)); fragment = fragment.replace(/\?(.*)$/, ''); fragment = this.root != '/' ? fragment.replace(this.root, '') : fragment; } else { let match = window.location.href.match(/#(.*)$/); fragment = match ? match[1] : ''; } return this.clearslashes(fragment); }
在这两种情况下,我们都在使用全局的window.location对象。在history的mode中,需要删除url的根部分。除此之外,还应该删除所有的get参数,所以用正则(/\?(.*)$/)来完成。正如大家所看到的,hash的值获取就简单多了。注意clearslashes()函数的使用方法。它的工作就是从字符串的开头和结尾删除/。这一点也是非常必要的,因为我们不想强制开发人员使用特定的url格式。不管开发者传递的是什么,都将处理成同样的值。
clearslashes: function (path) { return path.tostring().replace(/\$/, '').replace(/^\//, '') }
在实际工作中提供的东西总应该尽量给开发人员提供尽可能多的可控制的功能。在几乎所有的路由器实现中,路由被定义为字符串。不过,我更喜欢直接使用一个正则表达式。因为这样更为灵活。
同样的,开发者在使用一个路由器时,他能方便的添加和删除。也就是说,需要加上添加和删除路由的功能。
add: function (re, handler) { if (typeof re == 'function') { handler = re; re = ''; } } remove: function (param) { for (let i = 0; i < this.routes.length, r = this.routes[i]; i++) { if (r.handler === param || r.re.tostring() === param.tostring()) { this.routes.splice(i, 1); return this; } } return this; }
有时候,我们可能需要重新初始化类。因此,在这种情况下,可以像下面这样使用flush方法:
flush: function () { this.routes = []; this.mode = null; this.root = '/; return this }
我们有一个添加和删除url的api。我们也能得到当前的url。因此,下一步要做的就是比较注册的条目:
check: function (f) { let fragment = f || this.getfragment(); for (let i = 0; i < this.routes.length; i++) { let match = fragment.match(this.routes[i].re); if (match) { match.shift(); this.routes[i].handler.apply({}, match); retrun this; } } return this; }
我们使用getfragment方法或接受它作为函数的参数来获取片段。在此之后,我们在routes上执行一个正常的循环,并试图找到相匹配的。如果正则表达式不匹配,则有一个值 为null相匹配。否则它的值是这样的:
["products/12/edit/22", "12", "22", index: 1, input: "/products/12/edit/22"]
像数组一样的对象,它包含匹配的字符串和所有可记住的子字符串。这意味着,如果我们改变第一个元素,将得到一个动态的数组。例如:
router.add(/about/, function (){ console.log(about) }) .add(/products\/(.*)\/edit\/(.*)/, function(){ console.log('products', arguments) }) .add(function(){ console.log('default') }) .check('/products/12/edit/22')
输出的结果:
products ["12", "22"]
这就是如何处理动态url的方法。
在实际的项目当中,我们不可能一直运行这个方法来检测。那么就需要一个提供另外的东西,比如说添加一个逻辑,它会通知我们的地址栏的变化,甚至包括点击浏览器的后退或前进按钮等操作。在history api有一个popstate事件。当url更改时,将会触发这个事件。然而,有一些浏览器,在页面加载时就会发送这个事件。这样一来,我想了另一个解决办法。使用setinterval来写一个监视,即使mode的值设置为hash。
listen: function () { let self = this; let current = self.getfragment(); let fn = function () { if (current !== self.getfragment()) { current = self.getfragment(); self.check(current); } } clearinterval(this.interval); this.interval = setinterval(fn, 50); return this; }
我们需要保留最新的url,以便我们能够将它与新的url进行比较。
最后,我们的路由器需要一个能改变当前地址的函数,当然也需要触发路由的处理器。
navigate: function (path) { path = path ? path : ''; if (this.mode === 'history') { history.pushstate(null, null, this.root + this.clearslashes(path)); } else { window.location.href = window.location.href.replace(/#(.*)$/. '') + '#' + path; } return this; }
同样,根据mode属性做不同的事情。如果history api可用,我们就使用pushstate,否则就使用window.location。
到此,一个路由器就完成了,最终的代码如下:
let router = { routes: [], mode: null, root: '/', config: function (options) { this.mode = options && options.mode && (options.mode == 'history') && !!(history.pushstate) ? 'history' : 'hash'; this.root = options && options.root ? '/' + this.clearslashes(options.root) + '/' : '/' return this; }, getfragment: function () { let fragment = ''; if (this.mode === 'history') { fragment = this.clearslashes(decodeuri(location.pathname + location.search)); fragment = fragment.replace(/\?(.*)$/, ''); fragment = this.root != '/' ? fragment.replace(this.root, '') : fragment; } else { let match = window.location.href.match(/#(.*)$/); fragment = match ? match[1] : ''; } return this.clearslashes(fragment); }, clearslashes: function (path) { return path.tostring().replace(/\/$/, '').replace(/^\//, ''); }, add: function (re, handler) { if (typeof re == 'function') { handler = re; re = ''; } this.routes.push({ re: re, handler: handler }); return this; }, remove: function (param) { for (let i = 0; i < this.routes.length, r = this.routes[i]; i++) { if (r.handler === param || r.re.tostring() === param.tostring()) { this.routes.splice(i, 1); return this; } } return this; }, flush: function () { this.routes = []; this.mode = null; this.root = '/'; return this; }, check: function (f) { let fragment = f || this.getfragment(); for (let i = 0; i < this.routes.length; i++) { let match = fragment.match(this.routes[i].re); if (match) { match.shift(); this.routes[i].handler.apply({}, match); return this; } } return this; }, listen: function () { let self = this; let current = self.getfragment(); let fn = function () { if (current !== self.getfragment()) { current = self.getfragment(); self.check(current); } } clearinterval(this.interval); this.interval = setinterval(fn, 50); return this; }, navigate: function (path) { path = path ? path : ''; if (this.mode === 'history') { history.pushstate(null, null, this.root + this.clearslashes(path)); } else { window.location.href = window.location.href.replace(/#(.*)$/, '') + '#' + path; } return this; } }// 配置router.config({mode: 'history'});// 返回到初始状态router.navigate()// 添加路由router.add(/about/, function () { console.log('about'); }) .add(/products\/(.*)\/edit\/(.*)/, function () { console.log('products', arguments); }) .add(function(){ console.log('default'); }) .check('/products/12/edit/22').listen();// 转发router.navigate('/about')
示例2:使用20行代码写一个web路由上面我们看完了@krasimirtsonev用100行代码写的web路由。接下来,咱位再看看@joakim carlstein是怎么用20行代码写的一个web路由。
@joakim carlstein想出20行代码创建一个简单客户端路由的思路主要来源于用20行代码写一个模板,而这个灵感却又来自于john resig在同一主题上的文章。是不是很有趣,很鼓舞人心。
@joakim carlstein是怎么用20行代码写一个路由。如果你对这方面感兴趣的话,请继续往下阅读。
首先创建一个html模板:
<!doctype html><html> <head> <meta charset="utf-8" /> <title>创建一个web路由</title> <script> // johe的模板引擎代码放在这里 // http://ejohn.org/blog/javascript-micro-templating/ </script> </head></html>
模板中使用了<script>标签,并且设置type="text/html"。这将使浏览器不解析它们的内容,这也是我们想要的那样。
<script type="text/html" id="home"> <h1>router ftw!</h1></script><script type="text/html" id="template1"> <h1>page 1: <%= greeting %></h1> <p><%= moretext %></p></script><script type="text/html" id="template2"> <h1>page 2: <%= heading %></h1> <p>lorem ipsum...</p></script>
正如你所看到的,这是非常基础的部分,那是因为我们把主要精力会放在路由器的那部分,所以不想花太多的精力在模板上面。
对于这个路由,作者将使用url的hash,也就是url中#符号后面的那部分。例如http://example.com/#our/url/here中的our/url/here。本来可以使用html的history api,但这个示例中将不采用这个。
路由器将使用onhashchange事件来处理页面加载后的路由更改和通常的onload事件,用来处理页面加载url的任何路由。
先从注册路由函数开始:
// 使用一个hash来存储我们的路由let routes = {}// 路由注册函数function route (path, templateid, controller) { routes[path] = { templateid: templateid, controller: controller } }
现在我们就可以创建新的路由。请注意,下面的代码模仿了angularjs的控制器:
route('/', 'home', function () {}); route('/page1', 'template1', function () { this.greeting = 'hello world!'; this.moretext = 'bacon ipsum...'; }); route('/page2', 'template2', function () { this.heading = 'i\'m page two!'; });
结果上面的代码并没有起任何的作用,那是因为我们还没有处理好路由。要上面的代码能有作用,就需要添加路由的处理程序。也就是说要构建路由器。但是首先需要一个地方来呈现我们的页面。
let el = null;function router () { // 延迟加载view元素 el = el || document.getelementbyid('view'); // 当前路由url, 删除`#` let url = location.hash.clice(1) || '/'; let router = routes[url]; if (el && route.controller) { // 使用john resig的模板引擎,渲染路由模板 el.innerhtml = tmpl(route.templateid, new route.controller()); } }// 监听hash的变化window.addeventlistener('hashchange', router);// 监听页面加载window.addeventlistener('load', router);
这就是最基本的路由。另外,导航中的链接应该也能工作,也就是说可以通过浏览器直接进入特定的路由。比如path/to/your/router.html#/page1,你应该可以看到page1对应的内容。
为了使路由器变得理有用,可以添加单向数据绑定,以便在控制器中数据发生变化时自动更新视图。这需要使用到object.observe()。
在上面的路由函数上添加一个对象观察者,它会得新运行当前视图:
let el = null, current = null;function router () { el = el || document.getelementbyid('view'); if (current) { object.unobserve(current.controller, current.render); current = null } let url = location.hash.slice(1) || '/'; let route = routes[url]; if (el && route.controller) { current = { controller: new route.controller, template: tmpl(route.templateid), render: function () { el.innerhtml = this.template(this.controller); } }; current.render(); object.observe(current.controller, current.render.bind(current)); } }
就这些代码了可以帮助我们实现单向数据绑定的功能。为了验证上面的路由是否有效,可以测试一下:
route('/page1', 'template1', function () { this.greeting = 'hello world!'; this.moretext = 'loading...'; settimeout(function () { this.moretext = 'bacon ipsum...'; }.bind(this), 500); });
总结上面两个示例,通过不上一百行的javascript代码,写出不同的web路由,是不是很有意思。虽然这些路由功能还不是非常的强,也有一定的缺陷。但对于帮助我们学习如何使用 javascript写web路由还是很有帮助的。如果你感兴趣,也可以尝试一下,用极少数的代码,写一个web路由。
相关推荐:
js路由跳转的简单方法
vue router动态路由和嵌套路由实例详解
js路由跳转的简单实现代码
以上就是javascript写web路由代码分享的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product