Skip to content

Commit e180fd9

Browse files
优化代码
1 parent 9e4805d commit e180fd9

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

example/html5History.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<head>
55
<title>qs-router</title>
6-
<base href="/">
6+
<base href="/qs-router">
77
</head>
88

99
<body>
@@ -27,7 +27,7 @@ <h1>html5History模式</h1>
2727
<script type="text/javascript" src="../router.js"></script>
2828
<script type="text/javascript">
2929
let router = new Router({
30-
base: '/qs-router/example/',
30+
base: '/qs-router',
3131
type: 'history',
3232
routes: [{
3333
path: '/',

router.js

+35-31
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
// TODO
66
},
77

8+
/**
9+
* history模式更改状态
10+
* @params {string} url - 需要更改的url
11+
* @params {boolen} replace - 区分是替换还是新增的方式
12+
*/
813
pushState: function(url, replace) {
914
const history = window.history;
1015

@@ -25,27 +30,33 @@
2530
window.location.hash = hash;
2631
},
2732

33+
// 生成一个唯一KEY
2834
getKey: () => {
2935
const Time = window.performance && window.performance.now ? window.performance : Date;
3036
return parseInt(Time.now());
3137
},
3238

39+
/**
40+
* 根据用户配置生成路由映射map
41+
* @params {object} object - 用户配置的路由信息对象
42+
* @params {string} key - 二级路径
43+
*/
3344
map: (object, key) => {
3445
object.forEach(item => {
3546
let router = key;
3647
if ("object" === typeof item.component) {
37-
let object = {
48+
let item = {
3849
id: '',
3950
template: '',
4051
before: function() {},
4152
after: function() {}
4253
}
4354

44-
object.id = item.component.id || 'template';
45-
object.template = item.component.template;
55+
item.id = item.component.id || 'template';
56+
item.template = item.component.template;
4657

47-
'function' === typeof item.component.beforeRouteUpdate ? object.before = item.component.beforeRouteUpdate : '';
48-
'function' === typeof item.component.afterRouteUpdate ? object.after = item.component.afterRouteUpdate : '';
58+
'function' === typeof item.component.beforeRouteUpdate ? item.before = item.component.beforeRouteUpdate : '';
59+
'function' === typeof item.component.afterRouteUpdate ? item.after = item.component.afterRouteUpdate : '';
4960

5061
if (item.path.charAt(item.path.length - 1) !== '/' && router.charAt(router.length - 1) !== '/') {
5162
router = `${key}/${item.path}`;
@@ -55,7 +66,7 @@
5566
router = `${item.path}`
5667
}
5768

58-
_router[router] = object;
69+
_router[router] = item;
5970
}
6071

6172
if (item.hasOwnProperty('children') && item.children.length) {
@@ -88,6 +99,12 @@
8899
return `${base}#${path}`;
89100
},
90101

102+
/**
103+
* 渲染函数
104+
* @params {string} id - 容器id
105+
* @params {string} template - 模板内容
106+
* @params {function} callback - 回调函数
107+
*/
91108
render: (id, template, callback) => {
92109
let dom = document.getElementById(id);
93110

@@ -141,14 +158,6 @@
141158
}
142159
},
143160

144-
onReady: (successCallBack, errorCallBack) => {
145-
146-
},
147-
148-
onError: callback => {
149-
150-
},
151-
152161
push: function(params) {
153162

154163
if ('string' === typeof params) {
@@ -201,14 +210,6 @@
201210
util.replaceHash(url);
202211
},
203212

204-
onReady: (successCallBack, errorCallBack) => {
205-
206-
},
207-
208-
onError: callback => {
209-
210-
},
211-
212213
push: params => {
213214
if ('string' === typeof params) {
214215
util.pushHash(params);
@@ -224,6 +225,7 @@
224225
}
225226

226227
/**
228+
* qs-router 路由根对象
227229
* @params {Object} - Router
228230
*/
229231
function Router(params) {
@@ -249,6 +251,7 @@
249251
break;
250252
}
251253

254+
// 开始初始化路由映射
252255
util.map(this.routes, this.base);
253256
this.history.init();
254257
}
@@ -257,12 +260,16 @@
257260

258261
/**
259262
* 替换掉当前的history记录
260-
* @params {string} url
263+
* @params {string} url - 需要替换的URL
261264
*/
262265
replace: function(url) {
263266
this.history.replace(url, true);
264267
},
265268

269+
/**
270+
* 跳转到目标id
271+
* @params {number} index - 需要跳转到的目标索引
272+
*/
266273
go: function(index) {
267274
this.history.go(index);
268275
},
@@ -275,14 +282,11 @@
275282
this.go(1);
276283
},
277284

278-
onReady: function(successCallBack, errorCallBack) {
279-
this.history.onReady(successCallBack, errorCallBack);
280-
},
281-
282-
onError: function(callback) {
283-
this.history.onError(callback);
284-
},
285-
285+
/**
286+
* 通过JS的方式跳转到目标页面
287+
* '/' 或者 {path: '/'}
288+
* @params {object | string} params - 传入的跳转的参数
289+
*/
286290
push: function(params) {
287291
this.history.push(params);
288292
}

0 commit comments

Comments
 (0)