-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhengmz
committed
Mar 15, 2019
1 parent
fdd0d83
commit 7b36a60
Showing
4 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
node_modules | ||
*.log | ||
*.swp | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// UMD封装 | ||
(function (global, factory) { | ||
if (typeof exports === 'object' && typeof module === 'object') | ||
module.exports = factory(); | ||
else if (typeof define === 'function' && define.amd) | ||
define('Router', [], factory); | ||
else if (typeof exports === 'object') | ||
exports.Router = factory(); | ||
else | ||
global.Router = factory(); | ||
}(this, (function () { | ||
'use strict'; | ||
|
||
//构造函数 | ||
function Router() { | ||
this.version = '1.0.0'; | ||
} | ||
|
||
//内部变量与函数 | ||
var routes = [], | ||
mode = null, // 路由模式 | ||
root = window.location.pathname, // 路由根目录 | ||
// 清除前后/字符 | ||
clearSlashes = function (path) { | ||
return path.toString().replace(/\/$/, '').replace(/^\//, ''); | ||
}; | ||
|
||
//原型继承 | ||
Router.prototype = { | ||
// 配置 | ||
config: function (options) { | ||
mode = options && options.mode && options.mode == 'history' && | ||
!!(history.pushState) ? 'history' : 'hash'; | ||
//root = options && options.root ? '/' + clearSlashes(options.root) + '/' : '/'; | ||
// 支持对具体文件的路由如, 如/test/router/a.html | ||
root = options && options.root ? options.root : root; | ||
if (mode === 'history') { | ||
root = '/' + clearSlashes(root) + '/'; | ||
} | ||
return this; | ||
}, | ||
// 获取当前URL, 含查询内容 | ||
getFragment: function () { | ||
var fragment = ''; | ||
if (mode === 'history') { | ||
fragment = clearSlashes(decodeURI(location.pathname + location.search)); | ||
//删除?以后(含)的内容 | ||
fragment = fragment.replace(/\?(.*)$/, ''); | ||
//root会自动增加前后的'/'符号, 所以fragment也需要加, 然后替换 | ||
fragment = '/' + fragment + '/'; | ||
fragment = root != '/' ? fragment.replace(root, '') : fragment; | ||
} else { | ||
var match = window.location.href.match(/#(.*)$/); | ||
fragment = match ? match[1] : ''; | ||
} | ||
return clearSlashes(fragment); | ||
}, | ||
// 增加路由 | ||
add: function (re, handler) { | ||
if (typeof re == 'function') { | ||
handler = re; | ||
re = ''; | ||
} | ||
routes.push({ | ||
re: re, | ||
handler: handler | ||
}); | ||
return this; | ||
}, | ||
// 删除路由 | ||
remove: function (param) { | ||
for (var i = 0, r; r = routes[i], i < routes.length; i++) { | ||
if (r.handler === param || r.re.toString() === param.toString()) { | ||
routes.splice(i, 1); | ||
return this; | ||
} | ||
} | ||
return this; | ||
}, | ||
// 清除路由, 系统初始化 | ||
flush: function () { | ||
routes = []; | ||
mode = null; | ||
//root = '/'; | ||
root = window.location.pathname; | ||
return this; | ||
}, | ||
// 检测入口并处理URL | ||
check: function (f) { | ||
var fragment = f || this.getFragment(); | ||
for (var i = 0; i < routes.length; i++) { | ||
var match = fragment.match(routes[i].re); | ||
if (match) { | ||
match.shift(); | ||
routes[i].handler.apply({}, match); | ||
return this; | ||
} | ||
} | ||
return this; | ||
}, | ||
// 监控变化:使用这种方式可以支持到ie6, 用事件方式只能支持ie8+ | ||
listen: function () { | ||
var self = this; | ||
var current = self.getFragment(); | ||
var fn = function () { | ||
if (current !== self.getFragment()) { | ||
current = self.getFragment(); | ||
self.check(current); | ||
} | ||
}; | ||
clearInterval(this.interval); | ||
this.interval = setInterval(fn, 50); | ||
return this; | ||
}, | ||
// 改变当前URL以及触发处理 | ||
navigate: function (path) { | ||
path = path ? path : ''; | ||
if (mode === 'history') { | ||
history.pushState(null, null, root + clearSlashes(path)); | ||
} else { | ||
//window.location.href = window.location.href.replace(/#(.*)$/, '') + '#' + path; | ||
window.location.href = root + '#' + path; | ||
} | ||
return this; | ||
} | ||
}; | ||
|
||
//原型构造函数修改 | ||
Router.prototype.constructor = Router; | ||
//实例化 | ||
return new Router(); | ||
}))); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters