前端路由的簡易實現(xiàn)

  1. 前端路由實現(xiàn)
    前端路由實現(xiàn)的簡要原理,以 hash 形式(也可以使用 History API 來處理)為例,當 url 的 hash 發(fā)生變化時,觸發(fā) hashchange 注冊的回調,回調中去進行不同的操作,進行不同的內容的展示。直接看代碼或許更直觀。
function Router() {
    this.routes = {};
    this.currentUrl = '';
}
Router.prototype.route = function(path, callback) {
    this.routes[path] = callback || function(){};
};
Router.prototype.refresh = function() {
    this.currentUrl = location.hash.slice(1) || '/';
    this.routes[this.currentUrl]();
};
Router.prototype.init = function() {
    window.addEventListener('load', this.refresh.bind(this), false);
    window.addEventListener('hashchange', this.refresh.bind(this), false);
}
window.Router = new Router();
window.Router.init();
  1. 上面路由系統(tǒng) Router 對象實現(xiàn),主要提供三個方法
    init 監(jiān)聽瀏覽器 url hash 更新事件
    route 存儲路由更新時的回調到回調數(shù)組routes中,回調函數(shù)將負責對頁面的更新
    refresh 執(zhí)行當前url對應的回調函數(shù),更新頁面
  2. Router 調用方式以及呈現(xiàn)效果如下:點擊觸發(fā) url 的 hash 改變,并對應地更新內容(這里為 body 背景色)
<ul> 
    <li><a href="#/">turn white</a></li> 
    <li><a href="#/blue">turn blue</a></li> 
    <li><a href="#/green">turn green</a></li> 
</ul> 
var content = document.querySelector('body');
// change Page anything
function changeBgColor(color) {
    content.style.backgroundColor = color;
}
Router.route('/', function() {
    changeBgColor('white');
});
Router.route('/blue', function() {
    changeBgColor('blue');
});
Router.route('/green', function() {
    changeBgColor('green');      
});

以上為一個前端路由的簡單實現(xiàn),下面是完整代碼,雖然簡單,但實際上很多路由系統(tǒng)的根基都立于此,其他路由系統(tǒng)主要是對自身使用的框架機制的進行配套及優(yōu)化,如與 react 配套的 react-router。

  • 完整代碼
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>router</title>
</head>
<body>
    <ul> 
        <li><a href="#/">turn white</a></li> 
        <li><a href="#/blue">turn blue</a></li> 
        <li><a href="#/green">turn green</a></li> 
    </ul> 
<script>
    function Router() {
        this.routes = {};
        this.currentUrl = '';
    }
    Router.prototype.route = function(path, callback) {
        this.routes[path] = callback || function(){};
    };
    Router.prototype.refresh = function() {
        this.currentUrl = location.hash.slice(1) || '/';
        this.routes[this.currentUrl]();
    };
    Router.prototype.init = function() {
        window.addEventListener('load', this.refresh.bind(this), false);
        window.addEventListener('hashchange', this.refresh.bind(this), false);
    }
    window.Router = new Router();
    window.Router.init();
    var content = document.querySelector('body');
    // change Page anything
    function changeBgColor(color) {
        content.style.backgroundColor = color;
    }
    Router.route('/', function() {
        changeBgColor('white');
    });
    Router.route('/blue', function() {
        changeBgColor('blue');
    });
    Router.route('/green', function() {
        changeBgColor('green');
    });
</script>
</body>
</html>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,502評論 19 139
  • github地址,歡迎大家提交更新。 express() express()用來創(chuàng)建一個Express的程序。ex...
    Programmer客棧閱讀 2,808評論 0 1
  • vue2有著深度繼承的路由插件,即:vue-router,其中文的API地址。 vue-router與其他的路由(...
    白水螺絲閱讀 17,674評論 0 17
  • 很多事情你堅持做下去就能感染身邊的很多人,沒看小島書店之前,我覺得跑步和健身是這樣的,看了小島書店之后我發(fā)現(xiàn)原來讀...
    大哥今天一米八閱讀 398評論 0 0
  • 每一個人對待友情的方式都是不一樣的。每一個人把友情看重的位置也是不一樣的。緣分這個東西很奇妙,有時候會讓你...
    steven徐閱讀 288評論 0 0

友情鏈接更多精彩內容