我正在尝试在我的React.js组件的componentWillMount()中使用此函数。但是浏览器说window.decodeURIComponent(...)不是一个函数,我不明白。任何人都可以告诉我如何让它工作?感谢
export const getUrlQuery = window => {
let queryDict = {}
if (window.location.search) {
window.location.search.substr(1).split("&").forEach((item) => {
let s = item.split("=")
let key = s[0]
let value = s[1] && window.decodeURIComponent(s[1])
(queryDict[key] = queryDict[key] || []).push(value)
})
}
return queryDict
}
答案 0 :(得分:5)
有趣的错误,结果是let value = ..
行被解析为
let value = s[1] && window.decodeURIComponent(s[1])(queryDict[key] = queryDict[key] || []) ..
换句话说,您将括号表达式应用于window.decodeURIComponent(s[1])
的结果,它是字符串而不是函数。你可以用一个明确的分号来修复它(一般来说这不是一个坏主意):
let value = s[1] && window.decodeURIComponent(s[1]);
UPD:看看代码在做什么,你可能会对qs npm包感兴趣。
答案 1 :(得分:0)
删除这部分代码后,事情就有效了。
{{1}}
看起来这个语法错误引起了大喊而不是decodeURIComponent()