Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

让路由支持传参 #547

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions js/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,37 @@
*/
getUrlFragment: function(url) {
var hashIndex = url.indexOf('#');
return hashIndex === -1 ? '' : url.slice(hashIndex + 1);
if (hashIndex > -1) {
var fragment = url.slice(hashIndex + 1);
if (fragment.indexOf('?') > -1) {
fragment = fragment.slice(0, fragment.indexOf('?'));
}
return fragment;
}
return '';
},
/**
* 获取 url 中 hash 后面传入的参数
*
* 如果没有则返回空对象
* 如: http://example.com/path/?query=d#123?a=1&b=2 => {a:1, b:2}
*
* @param {String} url url
* @returns {Object}
*/
getUrlSearch: function(url) {
var fragment = url.slice(url.indexOf('#')),
fIndex = fragment.indexOf('?'),
i = 0, fs = [], search = {};
if (fIndex > -1) {
fragment = fragment.slice(fIndex + 1);
fs = fragment.split('&');
for (i; i < fs.length; i++) {
var _s = fs[i].split('=');
search[_s[0]] = _s[1];
}
}
return search;
},
/**
* 获取一个链接相对于当前页面的绝对地址形式
Expand Down Expand Up @@ -140,13 +170,15 @@
toUrlObject: function(url) {
var fullUrl = this.getAbsoluteUrl(url),
baseUrl = this.getBaseUrl(fullUrl),
fragment = this.getUrlFragment(url);
fragment = this.getUrlFragment(url),
search = this.getUrlSearch(url);

return {
base: baseUrl,
full: fullUrl,
original: url,
fragment: fragment
fragment: fragment,
search: search
};
},
/**
Expand Down Expand Up @@ -225,6 +257,8 @@
var $curVisibleSection = $visibleSection.eq(0);
var $hashSection;

this.$args = currentUrlObj;

if (currentUrlObj.fragment) {
$hashSection = $doc.find('#' + currentUrlObj.fragment);
}
Expand Down