【问题标题】:activeClassName not applying to Link when path is descendant to parent当路径是父级的后代时,activeClassName 不适用于链接
【发布时间】:2020-01-30 09:49:04
【问题描述】:

这是我的路线配置:

<Link className="nav-link " activeClassName="active" to={'/parent'}>
   parent
</Link>

当我访问网址时:https://localhost:8000/some-url/#/parent/ react 将类 active 应用于链接。但是当我访问路径时: https://localhost:8000/some-url/#/parent/child ,反应不应用 active 类。

当 url 是父级的任何后代时,有关如何将活动类应用于链接的任何建议。

我关注了documentation,上面写着

如果当前路线是链接的,则将处于活动状态 路线或链接路线的任何后代。要使链接仅在确切链接的路线上处于活动状态,请改用或设置 onlyActiveOnIndex 属性。

但不确定为什么默认情况下它没有被应用。

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    如果要使用 Link,您应该使用 NavLink,就像 documentation 所说的那样

    &lt;Link&gt; 的特殊版本,将添加样式属性到 与当前 URL 匹配时呈现的元素。

    通过使用NavLink,你应该能够实现这一点,你也可以使用exact、strict、isActive等东西

    【讨论】:

      猜你喜欢
      • 2020-11-01
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-29
      相关资源
      最近更新 更多