|
5 | 5 | // TODO
|
6 | 6 | },
|
7 | 7 |
|
| 8 | + /** |
| 9 | + * history模式更改状态 |
| 10 | + * @params {string} url - 需要更改的url |
| 11 | + * @params {boolen} replace - 区分是替换还是新增的方式 |
| 12 | + */ |
8 | 13 | pushState: function(url, replace) {
|
9 | 14 | const history = window.history;
|
10 | 15 |
|
|
25 | 30 | window.location.hash = hash;
|
26 | 31 | },
|
27 | 32 |
|
| 33 | + // 生成一个唯一KEY |
28 | 34 | getKey: () => {
|
29 | 35 | const Time = window.performance && window.performance.now ? window.performance : Date;
|
30 | 36 | return parseInt(Time.now());
|
31 | 37 | },
|
32 | 38 |
|
| 39 | + /** |
| 40 | + * 根据用户配置生成路由映射map |
| 41 | + * @params {object} object - 用户配置的路由信息对象 |
| 42 | + * @params {string} key - 二级路径 |
| 43 | + */ |
33 | 44 | map: (object, key) => {
|
34 | 45 | object.forEach(item => {
|
35 | 46 | let router = key;
|
36 | 47 | if ("object" === typeof item.component) {
|
37 |
| - let object = { |
| 48 | + let item = { |
38 | 49 | id: '',
|
39 | 50 | template: '',
|
40 | 51 | before: function() {},
|
41 | 52 | after: function() {}
|
42 | 53 | }
|
43 | 54 |
|
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; |
46 | 57 |
|
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 : ''; |
49 | 60 |
|
50 | 61 | if (item.path.charAt(item.path.length - 1) !== '/' && router.charAt(router.length - 1) !== '/') {
|
51 | 62 | router = `${key}/${item.path}`;
|
|
55 | 66 | router = `${item.path}`
|
56 | 67 | }
|
57 | 68 |
|
58 |
| - _router[router] = object; |
| 69 | + _router[router] = item; |
59 | 70 | }
|
60 | 71 |
|
61 | 72 | if (item.hasOwnProperty('children') && item.children.length) {
|
|
88 | 99 | return `${base}#${path}`;
|
89 | 100 | },
|
90 | 101 |
|
| 102 | + /** |
| 103 | + * 渲染函数 |
| 104 | + * @params {string} id - 容器id |
| 105 | + * @params {string} template - 模板内容 |
| 106 | + * @params {function} callback - 回调函数 |
| 107 | + */ |
91 | 108 | render: (id, template, callback) => {
|
92 | 109 | let dom = document.getElementById(id);
|
93 | 110 |
|
|
141 | 158 | }
|
142 | 159 | },
|
143 | 160 |
|
144 |
| - onReady: (successCallBack, errorCallBack) => { |
145 |
| - |
146 |
| - }, |
147 |
| - |
148 |
| - onError: callback => { |
149 |
| - |
150 |
| - }, |
151 |
| - |
152 | 161 | push: function(params) {
|
153 | 162 |
|
154 | 163 | if ('string' === typeof params) {
|
|
201 | 210 | util.replaceHash(url);
|
202 | 211 | },
|
203 | 212 |
|
204 |
| - onReady: (successCallBack, errorCallBack) => { |
205 |
| - |
206 |
| - }, |
207 |
| - |
208 |
| - onError: callback => { |
209 |
| - |
210 |
| - }, |
211 |
| - |
212 | 213 | push: params => {
|
213 | 214 | if ('string' === typeof params) {
|
214 | 215 | util.pushHash(params);
|
|
224 | 225 | }
|
225 | 226 |
|
226 | 227 | /**
|
| 228 | + * qs-router 路由根对象 |
227 | 229 | * @params {Object} - Router
|
228 | 230 | */
|
229 | 231 | function Router(params) {
|
|
249 | 251 | break;
|
250 | 252 | }
|
251 | 253 |
|
| 254 | + // 开始初始化路由映射 |
252 | 255 | util.map(this.routes, this.base);
|
253 | 256 | this.history.init();
|
254 | 257 | }
|
|
257 | 260 |
|
258 | 261 | /**
|
259 | 262 | * 替换掉当前的history记录
|
260 |
| - * @params {string} url |
| 263 | + * @params {string} url - 需要替换的URL |
261 | 264 | */
|
262 | 265 | replace: function(url) {
|
263 | 266 | this.history.replace(url, true);
|
264 | 267 | },
|
265 | 268 |
|
| 269 | + /** |
| 270 | + * 跳转到目标id |
| 271 | + * @params {number} index - 需要跳转到的目标索引 |
| 272 | + */ |
266 | 273 | go: function(index) {
|
267 | 274 | this.history.go(index);
|
268 | 275 | },
|
|
275 | 282 | this.go(1);
|
276 | 283 | },
|
277 | 284 |
|
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 | + */ |
286 | 290 | push: function(params) {
|
287 | 291 | this.history.push(params);
|
288 | 292 | }
|
|
0 commit comments