React Router - 在iFrame中呈现时隐藏页眉和页脚

时间:2017-11-20 13:26:07

标签: javascript reactjs iframe react-router

我有一个使用create-react-app构建的反应应用程序。这是我的index.js

export class MyRoutes extends React.Component{
    render(){
        return(
            <div>
                <Route exact path='/' component={ Home }/>
                <Route path='/team' component={ Team }/>
                <Route path='/career' component={ Career }/>
                <Route path='/faq' component={ Faq }/>
                <Route path='/contact' component={ Contact }/>
                <Switch>
                    <Route exact path='/jobs' component={ JobHome } />
                    <Route path='/jobs/alle' component={ JobList } />
                    <Route path='/jobs/:url' component={ JobDescription } />
                </Switch>
            </div>
        )
    }
}

ReactDOM.render(
    <Router>
        <div>
            <Responsive minWidth={768}>
                <Header/>
                <MyRoutes/>
            </Responsive>

            <Responsive maxWidth={767}>
                <MobileHeader
                    routes={<MyRoutes/>}
                />
            </Responsive>
            <Footer />
        </div>
    </Router>,
    document.getElementById('root')
);

我需要在外部网站上的iframe中呈现网址/jobs/:url。 如何在此视图中排除<Header/><Footer/>

3 个答案:

答案 0 :(得分:2)

这个解决方案最终对我有用:

const MyRoutes = () => (
    <div>
        <Route exact path='/' component={ Home }/>
        <Route path='/team' component={ Team }/>
        <Route path='/career' component={ Career }/>
        <Route path='/faq' component={ Faq }/>
        <Route path='/contact' component={ Contact }/>
        <Switch>
            <Route exact path='/jobs' component={ JobHome } />
            <Route path='/jobs/alle' component={ JobList } />
            <Route path='/jobs/:url' component={ JobDescription } />
        </Switch>
        <Route path='/xing_jobs/:url' component={ JobDescription }/>
    </div>
)

export let App = (props) => {
    let location = props.location;
    let isXingUrl = location.pathname && location.pathname.match(/^\/xing_jobs/);
    console.log('isXingUrl', isXingUrl);

    return (
        <div>
            <Responsive minWidth={768}>
                {!isXingUrl && <Header/>}
                <MyRoutes/>
            </Responsive>

            <Responsive maxWidth={767}>
                <MobileHeader
                    showHeader={!isXingUrl}
                    routes={<MyRoutes/>}
                />
            </Responsive>
            {!isXingUrl && <Footer/>}
        </div>
    )
}
App = withRouter(App);

ReactDOM.render(
    <Router>
        <App />
    </Router>,
    document.getElementById('root')
);

答案 1 :(得分:1)

export class MyRoutes extends React.Component{
    render(){
        let {path} = this.props.matc
        let routes = (
            <div>
              <Switch>
                <Route path='/team' component={Team}/>
                <Route path='/career' component={Career}/>
                <Route path='/faq' component={Faq}/>
                <Route path='/contact' component={Contact}/>
                <Route exact path='/jobs' component={JobHome}/>
                <Route path='/jobs/alle' component={JobList}/>
                <Route path='/jobs/:url' component={JobDescription}/>
                <Route exact path='/' component={ Home }/>
              </Switch>
            </div>
        )
        return (
          <div>
            <Responsive minWidth={768}>
                {path != '/jobs/:url' && <Header/>}
                {routes}
            </Responsive>

            <Responsive maxWidth={767}>
                <MobileHeader
                    routes={routes}
                />
            </Responsive>
            {path != '/jobs/:url' && <Footer/>}
        </div>
        )
    }
}




ReactDOM.render(
    <Router>
        <Route exact path='/' component={ MyRoutes }/>
    </Router>,
    document.getElementById('root')
);

答案 2 :(得分:0)

看起来正确的方法是将移动标题放在路由器中:

<Route path="/jobs/..." component={ComponentwithoutHeader}>
<Route path="/" component={Header}>
...Routes wit header
</>

在标题组件中使用它来呈现嵌套组件。 {this.props.children}

这样你可以在页眉和页脚中使用相同的组件,并在这两者之间渲染主体。

告诉它是否有效。 ;)