[英]console result is not correct? when call getUrlParam function
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',但是現在我得到的結果是'/ “?
稍微更改您的正則表達式:
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]));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.