我正在将React Router 4.3.1与React一起使用,我已经创建了一个Layout组件,我的不同内容组件可以正常显示{this.props.Children},但是我似乎无法成功将Footer组件放置在在所有Layout的底部,即使它位于新的div中并且在所有逻辑的“外部”,我的Footer组件也是从上到下的第三个窗体,底部是内容。我已经为此战斗了好几周,没有运气,这是我的代码。
路线组件。-
import React from 'react';
import { Switch, BrowserRouter, Route } from 'react-router-dom';
import Layout from './components/Layout';
import Home from './components/Home';
import Whoweare from './components/Whoweare';
import Services from './components/Services';
import Contact from './components/Contact';
import Errornotfound from './components/Errornotfound';
export default function Routes() {
return (
<BrowserRouter>
<div>
<Route component={ Layout }/>
<Switch>
<Route exact path='/' component={ Home } />
<Route path='/whoweare' component={ Whoweare } />
<Route path='/services' component={ Services } />
<Route path='/contact' component={ Contact } />
<Route component={ Errornotfound } />
</Switch>
</div>
</BrowserRouter>
);
}
最后,这是我简单的Layout组件。-
您可能会注意到,页脚是最后一个组件,尽管它没有显示在底部。
import React, { Component } from 'react';
import Navbars from './Navbars';
import Header from './Header';
import Banners from './Banners';
import Footer from './Footer';
export default class Layout extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return(
<div>
<col>
<Header/>
<div>
<col>
<Navbars/>
</col>
<Banners/>
</div>
<div>
<col>
{this.props.Children}
</col>
</div>
<Footer/>
</col>
</div>
);
}
}
谢谢您提前完成所有工作,以某种方式,即使在版式中没有新的div,我也找不到解决方案。
答案 0 :(得分:1)
当前Layout
没有任何孩子。您要将整个Switch
及其内容组件放在Layout
之后。
代替以下内容:
<Route component={ Layout }/>
<Switch>
<Route exact path='/' component={ Home } />
<Route path='/whoweare' component={ Whoweare } />
<Route path='/services' component={ Services } />
<Route path='/contact' component={ Contact } />
<Route component={ Errornotfound } />
</Switch>
您似乎打算做更多类似的事情:
<Layout>
<Switch>
<Route exact path='/' component={ Home } />
<Route path='/whoweare' component={ Whoweare } />
<Route path='/services' component={ Services } />
<Route path='/contact' component={ Contact } />
<Route component={ Errornotfound } />
</Switch>
</Layout>
您还需要修复{this.props.Children}
。应该为{this.props.children}
-一旦Layout
有了孩子,这将很重要。