Rumah  >  Artikel  >  hujung hadapan web  >  什么是前端路由及解释

什么是前端路由及解释

小云云
小云云asal
2018-02-10 10:19:484006semak imbas

什么是前端路由

所谓的前端路由,拥有这样一种能力:客户端浏览器可以不依赖服务端,根据不同的URL渲染不同的视图页面。

前端路由的存在合理性

在Ajax之剑还未亮出,前端仍处于襁褓之中的时候,路由的工作交给了后端。在进行页面切换的时候,浏览器发送不同的<span style="font-size: 14px;">url</span>请求;服务器接收到浏览器的请求时,通过解析不同的<span style="font-size: 14px;">url</span>去拼接需要的<span style="font-size: 14px;">html</span>或者模板,然后将结果返回给浏览器端进行渲染。

服务器端路由也是不落俗套的有利亦有弊。它的好处是安全性更高,更严格得控制页面的展现。这在某些场景中是很有用的,譬如下单支付流程,每一步只有在上一步成功执行之后才能抵达。这在服务器端可以为每一步流程添加验证机制,只有验证通过才返回正确的页面。那么前端路由不能实现每一步的验证?自然不是,姑且相信你的代码可以写的很严谨,保证正常情况下流程不会错,但是另一个不得不面对的事实是:前端是毫无安全性可言的。用户可以肆意修改代码来进入不同的流程,你可能会为此添加不少的处理逻辑。相较之下,当然是后端控制页面的进入权限更为安全和简便。

另一方面,后端路由无疑增加了服务器端的负荷,并且需要<span style="font-size: 14px;">reload</span>页面,用户体验其实不佳。

这样,前端路由就有用武之地了。首先,它的出现无疑减轻了服务器端的压力。特别是对于一个比较复杂的应用来讲,或者更确切的说,对于拥有一个复杂路由系统的应用来说,服务器端需要为每一个不同的url执行一段处理逻辑在高并发的情况下实在有点不堪重负;其次,页面的切换可以不需要刷新整个页面了,没有网络延迟,没有闪烁刷新,提升了用户体验。

前端路由实现方式

既然目标实现,我们需要解决的问题有哪些?我们可以将问题拆的稍微细一点,先制定一个亿的小计划,实现之后再进行下一步:)

  • 在页面不刷新的前提下实现<span style="font-size: 14px;">url</span>变化

  • 捕捉到<span style="font-size: 14px;">url</span>的变化,以便执行页面替换逻辑

如何实现更新<span style="font-size: 14px;">url</span>并且页面不刷新

正如前面所说,前端路由相较于后端路由的一个特点就是页面在不完全刷新的情况下进行视图的切换。页面<span style="font-size: 14px;">url</span>变了,但是并没有重新加载!看上去似乎有点不可思议,其实也没什么大不了。

试想将浏览器地址栏当做一个输入框,我们需要实现的就是改变输入框的<span style="font-size: 14px;">value</span>但是不触发请求页面的操作,这样就不会重新加载新页面。倘若输入框的值的变化和发送请求是一个原子操作,我们也就束手无策了。庆幸的是,只有当我们敲击了回车之后,请求才会被发送出去(这是显而易见的吧)。因此这就为我们修改地址栏的值而不触发页面请求刷新创造了条件。BOM是否有提供修改浏览器地址栏<span style="font-size: 14px;">url</span>而不触发请求操作的方法呢?

这里,存在两种满足需求的方式。一是利用<span style="font-size: 14px;">url</span>中的<span style="font-size: 14px;">hash</span>字段;二是使用HTML5提供的history API

<span style="font-size: 14px;">hash</span>方式

了解<span style="font-size: 14px;">http</span>协议就会知道,<span style="font-size: 14px;">url</span>的组成部分有很多,譬如协议、主机名、资源路径、查询字段等等,其中包含一个称之为片段的部分,以<span style="font-size: 14px;">#</span>为标识。

例如: <span style="font-size: 14px;">http://www.gmail.com/text/#123</span><span style="font-size: 14px;">123</span>便是<span style="font-size: 14px;">url</span>中的<span style="font-size: 14px;">hash</span>部分。

打开控制台,输入 <span style="font-size: 14px;">location.hash</span>,你可以得到当前<span style="font-size: 14px;">url</span><span style="font-size: 14px;">hash</span>部分(如果当前<span style="font-size: 14px;">url</span>不存在<span style="font-size: 14px;">hash</span>则返回空字符串)。接下来,输入 <span style="font-size: 14px;">location.hash = '123'</span>,会发现浏览器地址栏的<span style="font-size: 14px;">url</span>变了,末尾增加了<span style="font-size: 14px;">#123</span>字段,并且,页面没有被重新刷新。很显然,这很符合我们的要求。

history API

HTML5引入了一个<span style="font-size: 14px;">history</span>对象,包含了一套访问浏览器历史的API,可以通过<span style="font-size: 14px;">window.history</span>访问到它。

这里我们看上了它的两个API方法:<span style="font-size: 14px;">pushState</span><span style="font-size: 14px;">replaceState</span>

<span style="font-size: 14px;">history.replaceState(dataObj, title, url);<br/>history.pushState(dataObj, title, url);<br/></span>

若上所示,它们接收完全相同的参数,都是对浏览器的历史栈进行操作,将传递的url和相关数据压栈,并将浏览器地址栏的<span style="font-size: 14px;">url</span>替换成传入的<span style="font-size: 14px;">url</span>且不刷新页面(正中下怀!)。

By the way,不同的地方是<span style="font-size: 14px;">pushState</span> 将指定的<span style="font-size: 14px;">url</span>直接压入历史记录栈顶,而 <span style="font-size: 14px;">replaceState</span> 是将当前历史记录栈顶替换成传入的数据。

这两种方式都可以帮我们满足题设条件。采用哪一种方式除了主观喜好之外,还得依照客观事实:低版本的浏览器对于history API的兼容性不好,例如遇到了IE8,摆在眼前的道路似乎就别无选择了。

如何跟踪<span style="font-size: 14px;">url</span>变化

在浏览器端,跟踪表单属性的变化一般都采用事件监听机制,跟踪<span style="font-size: 14px;">url</span>的变化也不落俗套。

对于<span style="font-size: 14px;">hash</span>方式的前端路由,通常可以监听 <span style="font-size: 14px;">hashchange</span> 事件,在事件回调中处理相应的页面视图展示等逻辑。

此外,HTML5提供的 <span style="font-size: 14px;">popstate</span> 事件也会在<span style="font-size: 14px;">url</span><span style="font-size: 14px;">hash</span>发生改变时触发。也就是说如果可以忽略低版本浏览器,我们使用<span style="font-size: 14px;">hash</span>方式路由时也可以采用监听这个事件进行回调处理。

那么,如果是采用history API的形式呢?根据MDN的描述:

调用 <span style="font-size: 14px;">history.pushState()</span> 或者 <span style="font-size: 14px;">history.replaceState()</span> 不会触发 <span style="font-size: 14px;">popstate</span> 事件。<span style="font-size: 14px;">popstate</span> 事件只会在浏览器某些行为下触发, 比如点击后退按钮(或者在JavaScript中调用 <span style="font-size: 14px;">history.back()</span> 方法)。

这也就是说,我们在使用history API改变浏览器的<span style="font-size: 14px;">url</span>时,仍需要额外的步骤去触发 <span style="font-size: 14px;">popstate</span> 事件,例如调用 <span style="font-size: 14px;">history.back()</span><span style="font-size: 14px;">history.forward()</span> 等方法。

从兼容性上来讲,前面有提及<span style="font-size: 14px;">hash</span>的方式兼容性更好。然而,对于低版本的浏览器,例如IE6等等,不支持 <span style="font-size: 14px;">hashchange</span> 事件。这个时候我们只能通过 <span style="font-size: 14px;">setInterval</span> 设置心跳的方式去模拟 <span style="font-size: 14px;">hashchange</span>

<span style="font-size: 14px;">var oldHash = location.hash;var oldURL = location.href;<br/><br/>setInterval(function() {    var newHash = location.hash;    var newURL = location.href;    if (newHash !== oldHash && typeof window.onhashchange === &#39;function&#39;) {        // 执行onhashchange回调<br/>        window.onhashchange({            &#39;type&#39;: &#39;hashchange&#39;,            &#39;oldURL&#39;: oldURL,            &#39;newURL&#39;: newURL<br/>        });<br/><br/>        oldHash = newHash;<br/>        oldURL = newURL;<br/>    }<br/>}, 100);<br/></span>

一个简单实现

这里,给出一个很简单的实现:

<span style="font-size: 14px;">router.js</span>

<span style="font-size: 14px;">function FrontRouter() {    this.routes = {};<br/>    window.addEventListener(&#39;load&#39;, this.resolve.bind(this), false);<br/>    window.addEventListener(&#39;hashchange&#39;, this.resolve.bind(this), false);<br/>}<br/><br/>FrontRouter.prototype.route = function(path, callback) {    this.routes[path] = callback || function() {};<br/>};<br/><br/>FrontRouter.prototype.resolve = function() {    this.curHash = location.hash.slice(1) || &#39;/&#39;;    typeof this.routes[this.curHash] === &#39;function&#39; && this.routes[this.curHash]();<br/>};<br/></span>

<span style="font-size: 14px;">index.html</span>

<span style="font-size: 14px;"><ul>    <li><a href=&#39;#blue&#39;></a></li>    <li><a href=&#39;#yellow&#39;></a></li>    <li><a href=&#39;#red&#39;></a></li></ul><br/></span>

<span style="font-size: 14px;">index.js</span>

<span style="font-size: 14px;">var router = new FrontRouter();<br/><br/>router.route(&#39;blue&#39;, function() {<br/>    document.body.style.backgroundColor = &#39;blue&#39;;<br/>});<br/><br/>router.route(&#39;yellow&#39;, function() {<br/>    document.body.style.backgroundColor = &#39;yellow&#39;;<br/>});<br/><br/>router.route(&#39;red&#39;, function() {<br/>    document.body.style.backgroundColor = &#39;red&#39;;<br/>});<br/></span>

一点总结

应用场景

前端路由大部分的应用场景,就是我们现在熟知的单页应用SPA。

不存在纯前端路由

我们此前所描述的前端路由,建立在已经打开了一个初始页面基础之上,然后在这个页面之内进行页面替换。然而,我们如何进入这个初始页面?仅靠前端路由肯定是力所不及。我们至少要向后端发送一次http请求,接收所需要加载的页面不是吗?

所以,我们并不能抛弃后端路由部分。这也意味着,我们需要和后端确认各自的分工,哪些url归前端解析,哪些归后台解析。

Atas ialah kandungan terperinci 什么是前端路由及解释. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn