本文出自:
http://blog.csdn.net/wyk304443164
有两种方式获取:请用的第二种
/** * 获取url地址 * @param name */ common.getQueryString = function (name) { let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); let r = window.location.search.substr(1).match(reg); if (r !== null) return unescape(r[2]); return null; }; /** * 获取url地址--NEW ,如果该方法获取不到会重新用上面的方法获取 * @param _that * @param name * @returns {*} */ common.localQuery = function (_that, name) { let value = ''; if (!this.isEmpty(_that) && !this.isEmpty(_that.props) && !this.isEmpty(_that.props.location) && !this.isEmpty(_that.props.location.query)) { value = _that.props.location.query[name]; } if (this.isEmpty(value)) { value = this.getQueryString(name); } return value; }; /** * 判断是不是空的或者undefined * @param obj * @returns {boolean} */ common.isNull = function (obj) { return obj === null || typeof obj === 'undefined' || obj === undefined; }; /** * 判断是不是空的字符串 * @param obj * @returns {boolean} */ common.isEmpty = function (obj) { return this.isNull(obj) || obj === ''; };使用:
common.localQuery(this, 'id')如果用了hashHistory的话,后面会默认带一个 # 号,很明显,你不能删掉他,这时上面那个方法就不能获取到#/path?id=111 ,这边的id,需要用下面的那个方法,我已经做了处理,如果能用后面的就用后面的,用不了就前面的。因为咱们路由跳转的时候,参数都是加在#后面的。