Mar*_*röm 112 javascript reactjs eslint react-router
我正在使用React和Redux来开发一个webapp,当我启动我的项目时,我得到了这个:
Line 13: Unexpected use of 'location' no-restricted-globals
Search for the keywords to learn more about each error.
Run Code Online (Sandbox Code Playgroud)
我搜索了很多关于如何解决它的问题,但我找到的答案都没有帮助我,所以我转向Stack溢出.
有谁知道如何解决这个错误?我很感激能得到的所有帮助.
Cha*_*ger 290
尝试添加window
之前location
(即window.location
).
这是一个简单的,也许不是最好的解决方案,但它有效。
在您收到错误的行上方的行中,粘贴以下内容:
// eslint-disable-next-line no-restricted-globals
小智 5
使用react-router-dom
库。
useLocation
如果您使用功能组件,请从那里导入钩子:
import { useLocation } from 'react-router-dom';
然后将其附加到变量:
Const location = useLocation();
然后就可以正常使用了:
location.pathname
PS:返回的location
对象只有五个属性:
{ hash: "", key: "", pathname: "/" search: "", state: undefined__, }