当前位置:   article > 正文

vue-router的hash模式和history模式详解

vue-router中hash模式和history模式的实现原理
参考来源:
一、vue-router是什么

​ vue-router就是WebApp的链接路径管理系统。vue的单页面应用是基于路由和组件的,路由用于设定访问路径,并将路径和组件映射起来。传统的页面应用,是用一些超链接来实现页面切换和跳转的。在vue-router单页面应用中,则是路径之间的切换,也就是组件的切换。路由模块的本质 就是建立起url和页面之间的映射关系

问题:为什么不用a标签

二、vue-router实现原理
1.vue-router 默认 hash 模式

​ 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载。 hash(#)是URL 的锚点,代表的是网页中的一个位置,单单改变#后的部分,浏览器只会滚动到相应位置,不会重新加载网页,也就是说hash 出现在 URL 中,但不会被包含在 http 请求中,对后端完全没有影响,因此改变 hash 不会重新加载页面;同时每一次改变#后的部分,都会在浏览器的访问历史中增加一个记录,使用”后退”按钮,就可以回到上一个位置;所以说Hash模式通过锚点值的改变,根据不同的值,渲染指定DOM位置的不同数据。

问题:是怎么监听hash变化的——hashchange()

如何简单实现:

  1. Class关键字初始化一个路由.
  1. class Routers {
  2. constructor() {
  3. // 以键值对的形式储存路由
  4. this.routes = {};
  5. // 当前路由的URL
  6. this.currentUrl = '';
  7. }
  8. }
  9. 复制代码
  1. 实现路由hash储存与执行。在初始化完毕后我们需要思考两个问题:

    • 将路由的hash以及对应的callback函数储存
    • 触发路由hash变化后,执行对应的callback函数
    1. class Routers {
    2. constructor() {
    3. this.routes = {};
    4. this.currentUrl = '';
    5. }
    6. // 将path路径与对应的callback函数储存
    7. route(path, callback) {
    8. this.routes[path] = callback || function() {};
    9. }
    10. // 刷新
    11. refresh() {
    12. // 获取当前URL中的hash路径
    13. this.currentUrl = location.hash.slice(1) || '/';
    14. // 执行当前hash路径的callback函数
    15. this.routes[this.currentUrl]();
    16. }
    17. }
    18. 复制代码
  2. 监听对应事件,我们只需要在实例化Class的时候监听上面的事件即可.

  1. class Routers {
  2. constructor() {
  3. this.routes = {};
  4. this.currentUrl = '';
  5. this.refresh = this.refresh.bind(this);
  6. window.addEventListener('load', this.refresh, false);
  7. window.addEventListener('hashchange', this.refresh, false);
  8. }
  9. route(path, callback) {
  10. this.routes[path] = callback || function() {};
  11. }
  12. refresh() {
  13. this.currentUrl = location.hash.slice(1) || '/';
  14. this.routes[this.currentUrl]();
  15. }
  16. }
  17. 复制代码

完整示例:hash router的初步实现

  1. 此外还要实现回退功能等,hash router完整代码参考
2.vue-router 可选择 history模式

​ 由于hash模式会在url中自带#,如果不想要很丑的 hash,我们可以用路由的 history 模式,只需要在配置路由规则时,加入"mode: 'history'".

  1. //main.js文件中
  2. const router = new VueRouter({
  3. mode: 'history',
  4. routes: [...]
  5. })
  6. 复制代码

​ 这种模式充分利用了html5 history interface 中新增的 pushState() 和 replaceState() 方法。这两个方法应用于浏览器记录栈,在当前已有的 back、forward、go 基础之上,它们提供了对历史记录修改的功能。只是当它们执行修改时,虽然改变了当前的 URL ,但浏览器不会立即向后端发送请求。不过这种模式要玩好,还需要后台配置支持。因为我们的应用是个单页客户端应用,如果后台没有正确的配置,当用户在浏览器直接访问 outsite.com/user/id 就会返回 404,这就不好看了。所以呢,你要在服务端增加一个覆盖所有情况的候选资源:如果 URL 匹配不到任何静态资源,则应该返回同一个 index.html 页面,这个页面就是你 app 依赖的页面。

  1. export const routes = [
  2. {path: "/", name: "homeLink", component:Home}
  3. {path: "/register", name: "registerLink", component: Register},
  4. {path: "/login", name: "loginLink", component: Login},
  5. {path: "*", redirect: "/"}]
  6. 复制代码
2.1.history API介绍:

其中常用的只有几种:

  1. window.history.back(); // 后退
  2. window.history.forward(); // 前进
  3. window.history.go(-3); // 后退三个页面
  4. 复制代码

history.pushState用于在浏览历史中添加历史记录,但是并不触发跳转,此方法接受三个参数,依次为:

  1. state:一个与指定网址相关的状态对象,popstate事件触发时,该对象会传入回调函数。如果不需要这个对象,此处可以填null
  2. title:新页面的标题,但是所有浏览器目前都忽略这个值,因此这里可以填null
  3. url:新的网址,必须与当前页面处在同一个域。浏览器的地址栏将显示这个网址。
  4. 复制代码

history.replaceState方法的参数与pushState方法一模一样,区别是它修改浏览历史中当前纪录,而非添加记录,同样不触发跳转。

popstate事件,每当同一个文档的浏览历史(即history对象)出现变化时,就会触发popstate事件。

2.2.新标准下路由的实现:
  1. class Routers {
  2. constructor() {
  3. this.routes = {};
  4. this._bindPopState();
  5. }
  6. init(path) {
  7. history.replaceState({path: path}, null, path);
  8. this.routes[path] && this.routes[path]();
  9. }
  10. route(path, callback) {
  11. this.routes[path] = callback || function() {};
  12. }
  13. go(path) {
  14. history.pushState({path: path}, null, path);
  15. this.routes[path] && this.routes[path]();
  16. }
  17. _bindPopState() {
  18. window.addEventListener('popstate', e => {
  19. const path = e.state && e.state.path;
  20. this.routes[path] && this.routes[path]();
  21. });
  22. }
  23. }
  24. window.Router = new Routers();
  25. Router.init(location.pathname);
  26. const content = document.querySelector('body');
  27. const ul = document.querySelector('ul');
  28. function changeBgColor(color) {
  29. content.style.backgroundColor = color;
  30. }
  31. Router.route('/', function() {
  32. changeBgColor('yellow');
  33. });
  34. Router.route('/blue', function() {
  35. changeBgColor('blue');
  36. });
  37. Router.route('/green', function() {
  38. changeBgColor('green');
  39. });
  40. ul.addEventListener('click', e => {
  41. if (e.target.tagName === 'A') {
  42. e.preventDefault();
  43. Router.go(e.target.getAttribute('href'));
  44. }
  45. });
  46. 复制代码
3.使用路由模块来实现页面跳转的方式
  • 方式1:直接修改地址栏
  • 方式2:this.$router.push(‘路由地址’)
  • 方式3:<router-link to="路由地址"></router-link>

转载于:https://juejin.im/post/5caf0cddf265da03474def8a

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/524238
推荐阅读
相关标签
  

闽ICP备14008679号