Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

大神请进,react-router与面包屑的问题

项目中面包屑用的antd里的<Breadcrumb />组件,与react-router配合形成这样的效果:

从列表页点击员工名称进入详情页:

员工列表 / 员工详情

但痛苦的是 路由是这样写的:

<Router history={ hashHistory } >
    <Route path="/" component={App} >
        <Route path="/list" breadcrumbName="员工列表" components={List} />
        <Route path="/list/detail/:id" breadcrumbName="员工详情" components={Detail} />
    </Route>
</Router>

上面路由带来的麻烦是面包屑只能显示员工列表或者员工详情,没办法显示员工列表 / 员工详情

如果将路由改成:

 <Router history={ hashHistory } >
    <Route path="/" component={App} >
        <Route path="/list" breadcrumbName="员工列表" components={List} >
            <Route path="detail/:id" breadcrumbName="员工详情" components={Detail} />
        </Route>
    </Route>
</Router>  

那么员工详情不会被渲染,需要在List中添加this.props.children,但是会渲染成下面的方式:

<List>
    <Detail />
</List>

我该如何修改才能让面包屑显示成员工列表 / 员工详情 ??


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

简单,看看官方的实例,写法是第二种。

const Home = ({ routes, params, children }) => (
  <div className="demo">
    <div className="demo-nav">
      <Link to="/">Home</Link>
      <Link to="/apps">Application List</Link>
    </div>
    {children || 'Home Page'}
    <Alert style={{ margin: '16px 0' }} message="Click the navigation above to switch:" />
    <Breadcrumb routes={routes} params={params} />
  </div>
);

在有children的时候不显示当前组件的元素,比如你这就是不显示list。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...