我正在尝试在 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
}
最佳答案
有趣的错误,原来 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 包。
关于javascript - 解码URIComponent不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49179035/