lightUp
V2EX  ›  问与答

问个关于 react-router 的问题。

  •  
  •   lightUp · Jul 3, 2018 · 1759 views
    This topic created in 2892 days ago, the information mentioned may be changed or developed.

    刚刚在看 router,有几个不知道的地方。

    <NavLink exact replace activeClassName="active" to="/">页面 1</NavLink>
    <NavLink replace to="/about">页面 2</NavLink>
    
    <Route exact path="/" component={Home} /> 
    <Route path="/about" component={About} /> 
    

    +默认进来页面 1,并且 a 标签加上 active,如果我在 home 组件里面有嵌套路由。

    <NavLink exact activeClassName="active" replace to="/home/homeChild1">嵌套路由 1</NavLink>
    <NavLink activeClassName="active" replace to="/home/homeChild2">嵌套路由 2</NavLink>
    
    <Route path="/home/homeChild1" component={HomeChild1} />
    <Route path="/home/homeChild2" component={HomeChild2} />
    
    

    +现在点击了嵌套路由,上面按钮的 active 就没有了,请问要怎么让嵌套路由的第一个默认显示,并且加上 active,而且上一级的路由 active 同时存在呢?

    3 replies    2018-07-03 11:18:11 +08:00
    huijiewei
        1
    huijiewei  
       Jul 3, 2018
    主路由的 exact 去掉
    huijiewei
        2
    huijiewei  
       Jul 3, 2018
    PS: 不能编辑,是主 NavLink 的
    lightUp
        3
    lightUp  
    OP
       Jul 3, 2018
    @huijiewei 去掉的确 active 同时存在了,但是主路由里点击其他的 NavLink 时,不会切换 active 了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1019 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 22:12 · PVG 06:12 · LAX 15:12 · JFK 18:12
    ♥ Do have faith in what you're doing.