页脚组件在React Router 4和布局中的位置错误

时间:2019-02-04 21:42:02

标签: javascript reactjs layout react-router-v4 react-router-dom

我正在将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,我也找不到解决方案。

1 个答案:

答案 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有了孩子,这将很重要。