JS實(shí)現(xiàn)簡單路由器功能的方法
本文實(shí)例講述了JS實(shí)現(xiàn)簡單路由器功能的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
var wawa = {};
wawa.Router = function(){
function Router(){
}
Router.prototype.setup = function(routemap, defaultFunc){
var that = this, rule, func;
this.routemap = [];
this.defaultFunc = defaultFunc;
for (var rule in routemap) {
if (!routemap.hasOwnProperty(rule)) continue;
that.routemap.push({
rule: new RegExp(rule, 'i'),
func: routemap[rule]
});
}
};
Router.prototype.start = function(){
console.log(window.location.hash);
var hash = location.hash, route, matchResult;
for (var routeIndex in this.routemap){
route = this.routemap[routeIndex];
matchResult = hash.match(route.rule);
if (matchResult){
route.func.apply(window, matchResult.slice(1));
return;
}
}
this.defaultFunc();
};
return Router;
}();
var router = new wawa.Router();
router.setup({
'#/list/(.*)/(.*)': function(cate, id){
console.log('list', cate, id);
},
'#/show/(.*)': function(id){
console.log('show', id);
}
}, function(){
console.log('default router');
});
router.start();
希望本文所述對大家的javascript程序設(shè)計(jì)有所幫助。
- 用director.js實(shí)現(xiàn)前端路由使用實(shí)例
- director.js實(shí)現(xiàn)前端路由使用實(shí)例
- vue.js使用watch監(jiān)聽路由變化的方法
- VueJs路由跳轉(zhuǎn)——vue-router的使用詳解
- AngularJS路由實(shí)現(xiàn)頁面跳轉(zhuǎn)實(shí)例
- AngularJS 路由詳解和簡單實(shí)例
- react-router JS 控制路由跳轉(zhuǎn)實(shí)例
- 使用AngularJS對路由進(jìn)行安全性處理的方法
- AngularJS監(jiān)聽路由的變化示例代碼
- JS實(shí)現(xiàn)前端路由功能示例【原生路由】
相關(guān)文章
JavaScript使用readAsDataUrl方法預(yù)覽圖片
這篇文章主要為大家詳細(xì)介紹了JavaScript使用readAsDataUrl方法預(yù)覽圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
addEventListener()與removeEventListener()解析
這篇文章主要為大家詳細(xì)介紹了addEventListener()與removeEventListener(),用于處理指定和刪除事件處理程序操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
toString.call()通用的判斷數(shù)據(jù)類型方法示例
這篇文章主要給大家介紹了關(guān)于toString.call()通用的判斷數(shù)據(jù)類型方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
自用js開發(fā)框架小成 學(xué)習(xí)js的朋友可以看看
js實(shí)現(xiàn)鼠標(biāo)移動到圖片產(chǎn)生遮罩效果
HTML+JavaScript實(shí)現(xiàn)掃雷小游戲
IE6/7中g(shù)etAttribute獲取href/src 屬性(相對路徑0值與其它瀏覽器不同

