简体   繁体   English

NavLink react-router-dom 不工作(如果刷新页面工作)

[英]NavLink react-router-dom not working ( working if refresh page )

I have try to use NavLink in react-router-dom but something not working.我尝试在react-router-dom使用NavLink但有些东西不起作用。 Thanks for any help.谢谢你的帮助。

I'm in Profile page, i click <NavLink to="/adminboard/settings"> and it change the path to '/adminboard/settings' and activeClassName in Settings but i'm still in Profile page.我在个人资料页面,我单击<NavLink to="/adminboard/settings">并将路径更改为'/adminboard/settings''/adminboard/settings' activeClassName,但我仍在个人资料页面。 Then i refresh page, it's going to the Settings page.然后我刷新页面,它会进入设置页面。

my code :我的代码:

<AdminNav style={{ margin: '0px', padding: '0px', marginTop: '160px' }}>
    <Router style={{ margin: '0px', padding: '0px' }}>
        <Switch style={{ margin: '0px', padding: '0px' }}>
           <Route exact path="/adminboard/profile" component={AdminboardProfile} />
           <Route exact path="/adminboard/postsanpham" component={AdminPostSanPham} />
           <Route exact path="/adminboard/settings" component={AdminSetting} />
           <Route exact path="/adminboard/store" component={AdminStore} />
        </Switch>
    </Router>
</AdminNav>

my Nav code:我的导航代码:

<NavLink to="/adminboard/profile" activeClassName="active">
  <span>{faUserIcon}</span>
  <span>Profile</span>
</NavLink>
<NavLink to="/adminboard/postsanpham" activeClassName="active">
   <span>{postsanpham}</span>
   <span>Store</span>
</NavLink>
<NavLink to="/adminboard/store" activeClassName="active">
   <span>{faClipboardListIcon}</span>
   <span>Store</span>
</NavLink>
<NavLink to="/adminboard/settings" activeClassName="active">
   <span>{faToolboxIcon}</span>
   <span>Settings</span>
</NavLink>

I guess the problem is because you did not use the exact attribute as in我想问题是因为你没有使用确切的属性

<Route exact path='/category' component={Category}></Route>

exact will look for complete route ie '/adminboard/settings' if exact is not used it will stop looking further if a match is first found (it matches with 'adminboard' and will not look further)精确将寻找完整的路由,即“/adminboard/settings”,如果不使用精确,它会在第一次找到匹配时停止进一步查找(它与“管理板”匹配,不会进一步查找)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM