V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
lightUp
V2EX  ›  问与答

问个关于 react-router 的问题。

  •  
  •   lightUp · 2018-07-03 10:47:04 +08:00 · 970 次点击
    这是一个创建于 2125 天前的主题,其中的信息可能已经有所发展或是发生改变。

    刚刚在看 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 条回复    2018-07-03 11:18:11 +08:00
    huijiewei
        1
    huijiewei  
       2018-07-03 11:12:40 +08:00
    主路由的 exact 去掉
    huijiewei
        2
    huijiewei  
       2018-07-03 11:12:58 +08:00
    PS: 不能编辑,是主 NavLink 的
    lightUp
        3
    lightUp  
    OP
       2018-07-03 11:18:11 +08:00
    @huijiewei 去掉的确 active 同时存在了,但是主路由里点击其他的 NavLink 时,不会切换 active 了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2760 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 15:20 · PVG 23:20 · LAX 08:20 · JFK 11:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.