美文网首页
前端路由实现

前端路由实现

作者: _章鱼小丸子 | 来源:发表于2019-05-24 18:47 被阅读0次

前端路由一般分为两种方式

  • hash路由
  • H5 History路由

简单介绍下这两个路由:

hash路由

  1. 标志:hash路由的标志是带有#
  2. 原理:通过监听url中的hash值变化来进行路由跳转。
  3. 优势:兼容性更好,在老版IE中都有运行
  4. 问题:url中一直存在#,页面不够美观
实现
  1. 新增Route类
  2. 构造函数中增加实例属性routers用于存储路由,currentUrl用于保存当前页面地址。
  3. 构造函数中添加对路由变化的监听
  4. 新增注册函数route
  5. 新增回退函数back
  • 构造函数中新增history数组,用于保存历史hash
  • 构造函数中新增currentIndex实例属性,用于追踪历史hash
class Router {
    constructor() {
        this.routes = {}; //键值对形式存储路由信息
        this.currentUrl = ''; //当前Url
        this.history = []; //hash历史
        this.currentIndex =this.history.length - 1; //默认指向当前最新的hash
        window.addEventListener('load', this.refresh, false);
        window.addEventListener('hashchange', this.refresh, false);
        this.refresh = this.refresh.bind(this);
        this.back = this.back.bind(this);
    }
    route(path, calllback) {
        // 注册路由
        this.routes[path] = callback || function () {}
    }
    refresh() {
        // 获取除去#的hash值
        this.currentUrl = location.hash.slice(1);
        this.history.push(this.currentUrl);
        this.currentIndex++;
        this.routes[this.currentUrl] && this.routes[this.currentUrl]();
    }
    back() {
        // 如果当前指向历史最早的hash,不再回退
       this.currentIndex >= 0? (this.currentIndex = 0) : (this.currentIndex -= 1);
       this.currentUrl = this.history[this.currentIndex];
       this.routes[this.currentUrl] && this.routes[this.currentUrl]();
    }
}

H5 History路由

H5新增的History对象,里面有各种API,常用API包含以下3个:

window.history.back();       // 后退
window.history.forward();    // 前进
window.history.go(-3);       // 后退三个页面

在浏览器点击前进后退就会调用以上API。想要实现路由,还有几个重要的API需要我们知道:

  • history.pushState
    用于在浏览历史中添加历史记录,但是并不触发跳转,此方法接受三个参数,依次为:
    (1)state:一个与指定网址相关的状态对象。popstate事件触发时才会产生,如果不需要可以填null。
    (2)title:新页面的标题,一般为null。
    (3)url:新的网址,要求与当前页面处在同一个域。
  • history.replaceState
    方法参数与pushState方法一样,区别是它修改浏览历史中当前纪录,而非添加记录,同样不触发跳转。
  • popstate事件
    每当同一个文档的history对象出现变化时,就会触发popstate事件。仅仅调用pushState方法或replaceState方法并不会触发该事件。只有当用户点击浏览器倒退按钮和前进按钮,或使用js调用back、forward、go方法时才会触发。
实现
class Router {
    constructor() {
        this.routes = {};
        // 构造函数中监听popstate
        this.watchPopState();
        this.init = this.init.bind(this);
        this.go = this.go.bind(this);
        this.back = this.back.bind(this);
    }
    route(path, calllback) {
        // 注册路由
        this.routes[path] = callback || function () {}
    }
    init(path){
        history.replaceState({path: path}, null, path);
        this.routes[path] && this.routes[path]();
    }
    go(path) {
        history.pushState({path: path}, null, path);
        this.routes[path] && this.routes[path]();
    }
    back() {
        history.back();
    }
    watchPopState() {
        window.addEventListener('popstate', e => {
            const path = e.state && e.state.path;
            this.routes[path] && this.routes[path]();
        })
    }
}

使用方式如下:

window.Router = new Routers();

Router.init(location.pathname);
Router.route('/', function() {
    changeBgColor('yellow');
})

let link = document.querySelector('a');

link.addEventListener('click', e => {
    e.preventDefault();
    let href = e.target.getAttribute('href')
    Router.go(href);
}, false);

相关文章

  • 前端路由的简易实现

    前端路由实现前端路由实现的简要原理,以 hash 形式(也可以使用 History API 来处理)为例,当 ur...

  • vue路由

    今日目标 1.能够说出什么是路由2.能够说出前端路由的实现原理3.能够使用Vue-Router实现前端路由4.能够...

  • vue路由

    今日目标 1.能够说出什么是路由2.能够说出前端路由的实现原理3.能够使用Vue-Router实现前端路由4.能够...

  • 面试:谈谈对前端路由的理解?

    面试官想听到什么答案呢? 1、为什么会出现前端路由。 2、前端路由解决了什么问题。 3、前端路由实现的原理是什么。...

  • SPA 中前端路由原理与实现方式

    SPA 中前端路由原理与实现方式 通常 SPA 中前端路由有2中实现方式,本文会简单快速总结这两种方法及其实现: ...

  • 实现一个简易的前端路由

    实现一个简易的前端路由

  • vue权限路由实现的方法示例总结

    使用全局路由守卫 实现 前端定义好路由,并且在路由上标记相应的权限信息 前端全栈学习交流圈:866109386,面...

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

    在学习 vue-router 的代码之前,先来简单了解一下前端路由。 前端路由主要有两种实现方法: Hash 路由...

  • 04-15动态路由的实现与优化

    Vue中后台鉴权的另一种思路 - 动态路由的实现与优化 鉴权-前端路由 VS 鉴权-动态路由 前端路由鉴权相信只要...

  • mini路由

    es6实现mini路由 参考文章:面试官: 你了解前端路由吗?

网友评论

      本文标题:前端路由实现

      本文链接:https://www.haomeiwen.com/subject/awcnzqtx.html