为了实现路由为
<App>
<LFMusic/>
/*<LFCollections/>*/
</App>
的效果,我遇见了下面的问题:
当Route组件父元素为div等元素时link可以正常跳转,而为自定义组件时只有路由变化,页面不跳转刷新。这是route父元素为自定义组件的情况
<Router history={history}>
<App>
<Route path="/" component={LFMusic} exact/>
<Route path="/mycollections" component={LFCollections}/>
</App>
</Router>
而需要手动刷新浏览器才可以正常显示
但是当route父元素为div时,点击link可以直接跳转并刷新。对了link组件在App子组件LFMusic中下面是route父元素为div的情况
<Router history={history}>
<div>
<Route path="/" component={LFMusic} exact/>
<Route path="/mycollections" component={LFCollections}/>
</div>
</Router>
求助,萌新已经懵逼了。
同样踩了这个坑.
原因是 <Route> 和 <Link> 没有在同一个 <Router> 下.
解决方案: 将 <Route> 和 <Link> 放在同一个 <Router> 标签内.
如下: