代码之家  ›  专栏  ›  技术社区  ›  bier hier

为什么我会“意外使用”location“无限制全局变量”?

  •  2
  • bier hier  · 技术社区  · 7 年前

    在我的ReactJS代码中,我得到了:

    const {
      children,
      location: { pathname },
    } = this.props;
    
    let path = location.pathname;
    

    同时在该组件中使用React路由器模块。如何修复此错误?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Quoc-Anh Nguyen    7 年前

    你应该试试这个代码

    const {
      children,
      location: { pathname },
    } = this.props;
    
    let path = pathname;
    

    你的 location 此块代码中未定义变量,因此 eslint 认为使用全局变量

        2
  •  0
  •   Hai Pham    7 年前

    因为 eslint 检测 location 是的元素 window . 因此,请尝试重命名 props :

    const {
      children,
      myLocation: { pathname },
    } = this.props;
    
    let path = myLocation.pathname;
    

    参考文献:

    推荐文章