• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

vue-router 源码实现前端路由的两种方式

vue 搞代码 4年前 (2022-01-08) 16次浏览 已收录 0个评论

这篇文章主要介绍了vue-router 源码实现前端路由的两种方式,主要介绍Hash 路由和History 路由,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

在学习 vue-router 的代码之前,先来简单了解一下前端路由。

前端路由主要有两种实现方法:

  • Hash 路由
  • History 路由

先来看看这两种方法的实现原理。

接着我们将用它们来简单实现一个自己的前端路由。

前端路由

Hash 路由

url 的 hash 是以 # 开头,原本是用来作为锚点,从而定位到页面的特定区域。当 hash 改变时,页面不会因此刷新,浏览器也不会向服务器发送请求。

http://www.xxx.com/#/home

同时, hash 改变时,并会触发相应的 hashchange 事件。所以,hash 很适合被用来做前端路由。当 hash 路由发生了跳转,便会触发 hashchange 回调,回调里可以实现页面更新的操作,从而达到跳转页面的效果。

 window.addEventListener('hashchange', function () { console.log('render'); });

History 路由

HTML5 规范中提供了 history.pushStatehistory.replaceState 来进行路由控制。通过这两个方法,可以实现改变 url 且不向服务器发送请求。同时不会像 hash 有一个 # ,更加的美观。但是 History 路由需要服务器的支持,并且需将所有的路由重定向到根页面。

History 路由的改变不会去触发某个事件,所以我们需要去考虑如何触发路由更新后的回调。

有以下两种方式会改变 url:

  • 调用 history.pushState 或 history.replaceState;
  • 点击浏览器的前进与后退。

第一个方式可以封装一个方法,在调用 pushState(replaceState)后再调用回调。

 function push (url) { window.history.pushState({}, null, url); handleHref(); } function handleHref () { console.log('render'); }

第二个方式,浏览器的前进与后退会触发 popstate 事件。

 window.addEventListener('popstate', handleHref);

路由实现

我们通过 标签来进行切换路由,通过一个

标签来装载各路由对应的页面内容。

参考 vue-router 的调用,我们会这么地调用一个 Router ,将路由与对应组件作为参数传入:

 const router = new Router([ { path: '/', component: 'home' }, { path: '/book', component: 'book' }, { path: '/movie', component: 'movie' } ]);

数组里是各路由对应的要显示的内容,接下来就来开始实现这个 Router

Hash 路由实现

Hash 路由 标签都需要带上 #

 <div> homebookmovie<div id="content"></div></div>

Router 的代码实现如下:

 class Router { constructor (options) { this.routes = {}; this.init(); // 遍历,绑定视图更新 options.forEach(item => { this.route(item.path, () => { document.getElementById('content').innerHTML = item.component; }); }); } // 绑定监听事件 init () { window.addEventListener('load', this.updateView.bind(this), false); window.addEventListener('hashchange', this.updateView.bind(this), false); } // 更新试图 updateView () { const currentUrl = window.location.hash.slice(1) || '/'; this.routes[currentUrl] && this.routes[currentUrl](); } // 将路由与回调函数关联 route (path, cb) { this.routes[path] = cb; } }

实现效果如下:

 

History 路由实现

History 路由需要服务器的支持,可以点击这里 的代码参考。

 <div> homebookmovie<div id="content"></div></div>

Router 的代码实现如下:

 class Router { constructor (options) { this.routes = {}; this.init(); this.bindEvent(); // 遍历,绑定视图更新 options.forEach(item => { this.route(item.path, () => { document.getElementById('content').innerHTML = item.component; }); }); } // 绑定点击事件 bindEvent () { const _this = this; const links = document.getElementsByTagName('a'); [].forEach.call(links, link => { link.addEventListener('click', function () { const url = this.getAttribute('data-href'); _this.push(url); }); }); } // 绑定监听事件 init () { window.addEventListener('load', this.updateView.bind(this), false); window.addEventListener('popstate', this.updateView.bind(this), false); } push (url) { window.history.pushState({}, null, url); this.updateView(); } // 更新试图 up<i>本文来源gaodai$ma#com搞$$代**码网</i>dateView () { const currentUrl = window.location.pathname || '/'; this.routes[currentUrl] && this.routes[currentUrl](); } // 将路由与回调函数关联 route (path, cb) { this.routes[path] = cb; } }

实现效果如下:

最后

前端路由实现方式有两种,分别是:

  1. Hash 路由
  2. History 路由

原理都是修改 url 的同时不刷新页面,不向服务器发送请求,通过监听特殊的事件来更新页面。

以上实现全部源码参考这里。

以上就是vue-router 源码实现前端路由的两种方式的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:vue-router 源码实现前端路由的两种方式

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址