【问题标题】:react js breadcrumbs反应js面包屑
【发布时间】:2018-10-15 23:15:41
【问题描述】:

在我的带有反应路由器的 reactJS 应用程序中,我正在尝试制作动态面包屑。

我试过这个:

const Breadcrumbs = () => <Route path="*" render={props => {
    let parts = props.location.pathname.split("/");
    const place = parts[parts.length-1];
    parts = parts.slice(1, parts.length-1);
    return <p>{parts.map(crumb)}/{place}</p>}} />;

const crumb = (part, partIndex, parts) => {
    const path = ['', ...parts.slice(0, partIndex+1)].join("/");
    return <Link key={path} to={path} >{part}</Link>};

但我只有一个/

也许是因为我的网址中有一个#? (即http://localhost:8080/#/products/

对于简单的面包屑有什么好的解决方案?

提前致谢

更新:

<Provider store={store}>
                <AppContainer>
                    <Router history={history}>
    <div>
                            <div className={to.topBar}>
                                <TopBarHelper changeRole={this.changeRole} currentRole={this.state.currentRole} users={this.state.users}/>
                            </div>
                            <div className={css.sidebararound}>
                                <div className={css.sidebar}>
                                    <ul>
                                        <li><NavLink to="/dashboard" activeClassName={css.activeSidebar} className="fa fa-bars fa-2x"/>
                                            <ul className={css.SubMenu}>
                                                <li><NavLink to="/dashboard" activeClassName={css.activeSidebar} aria-hidden="true">{t('sidebar:dashboard.tooltip')}</NavLink></li>
                                            </ul>
                                        </li>

[...]

<ThemeProvider theme={theme}>
                                <MuiThemeProvider muiTheme={amTheme}>
                                    <div className={i.content}>
                                        {this.props.loaded ?
                                            <Switch>
                                                <Route path="/folders" currentRole={this.state.currentRole} component={Folders}/>
                                                    <Route exact path="/" component={props => <DashboardRoutes {...props} currentRole={this.state.currentRole} users={this.state.users} client={this.state.clientId}/>}/>

【问题讨论】:

    标签: reactjs react-router breadcrumbs


    【解决方案1】:

    也许您可以使用window.location.hash 来简化对 url 路径部分的提取?

    所以,大致是这样的:

    const Breadcrumbs = () => <Route path="*" render={ props => {
    
        const hash = window.location.hash
        const parts = hash.split('/').filter(part => part.indexOf('#') === -1) // Removes first # part
        let path = ''
    
        return <p>{ parts.map((part, index) => {
    
          // Append part to current path
          path += '/' + part 
    
          return <Link key={index} to={path}>{part}</Link>    
        }) }
        </p>
    }} />;
    

    【讨论】:

    • 谢谢,我明白了:Cannot read property 'split' of undefined
    • 输出是&lt;p&gt;&lt;a href="#/"&gt;&lt;/a&gt;&lt;/p&gt;
    • 好吧,看起来更好,也许需要提出更多要求;)结果现在是&lt;p&gt;&lt;a href="#/products"&gt;products&lt;/a&gt;&lt;a href="#/products/edit"&gt;edit&lt;/a&gt;&lt;a href="#/products/edit/20"&gt;20&lt;/a&gt;&lt;/p&gt; 看起来不错,谢谢。是否有可能自动更改名称?在我的导航栏中,这些名称不同。但是现在我们只使用 URL 作为名称而不是给定的。
    • 当我将您的过滤器添加到以前的解决方案中时,我得到了相同的解决方案:.filter(part =&gt; part.indexOf('#') === -1) // Removes first # part
    • @Felix 很高兴听到它正在工作!好的,当然 - 这将涉及从应用程序的其他地方获取数据,并使用它来呈现名称标签 - 那些导航栏标签是从 redux 商店中的状态生成的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多