Utils.js:
// getUrlParam() is get location function
getUrlParam(name){
let queryString = window.location.search.split('?')[1],
reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'),
result = queryString.match(reg);
return decodeURIComponent(result[2])
}
登录组件:
class Login extends Component{
constructor(props){
super(props)
this.state = {
redirect: _utils.getUrlParam('redirect')
}
}
}
// _user.login() is backend login interface
// UserInfo include username and password
login(){
_user.login(UserInfo).then((res) => {
console.log(this.state.redirect)
})
}
当我点击"按钮",如果位置是" http://localhost:8088/login?redirect=%2Fcategory%2Findex",我希望得到的结果是' / category / index' ,但现在我得到的结果是' /'?
答案 0 :(得分:1)
稍微改变你的正则表达式:
var name = 'redirect';
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
var url = 'http://localhost:8088/login?redirect=%2Fcategory%2Findex';
var queryString = url.split('?')[1];
var match = queryString.match(reg);
console.log(decodeURIComponent(match[2]));