2017-10-04 36 views
0

我有如下的结构阵营:为什么其他组件不能在ReactJS的布局内渲染?

client.js // routes are defined here and rendered app 

    components // a directory with all header, footer, layout, etc components 
    | 
    \____ Header.js 
    \____ Layout.js 
    \____ Nav.js 
    \____ Setting.js 

    index.html 

现在的Layout.js里面我有:

import React from 'react'; 
import Footer from './Footer'; 
import Nav from './Nav'; 
import { Link, NavLink } from "react-router-dom"; 

export default class Layout extends React.Component { 
    constructor() { 
     super(); 
    } 
render() { 
    return (
    <div style={{textAlign: 'center'}}> 
     <Nav /> 
     <div className="container" style={{marginTop: '60px'}}> 
       {this.props.children} 
     </div> 
     <Footer /> 
     </div> 
    ); 
    } 
} 

client.js是如下:

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import Archive from './components/Archive.js'; 
import Layout from './components/Layout.js'; 
import Featured from './components/Featured.js'; 
import Setting from './components/Setting.js'; 
import { HashRouter, Route, Link } from 'react-router-dom'; 


ReactDOM.render(
     (<HashRouter> 
       <div> 
         <Route path="/" component={Layout} /> 
         <Route path="/archive/:aid?" component={Archive} /> 
         <Route path="/featured" component={Featured} /> 
         <Route path="/setting" component={Setting} /> 
       </div> 
     </HashRouter>), 
document.getElementById('app')); 

的问题是,当我通过链接导航,它呈现archive之后的内容Footer

enter image description here

NB:I'm in archive page是页脚部分后呈现

这里有什么问题?我该怎么做才能在布局中渲染组件?

+0

删除风格= {{marginTop: '60像素'}}在容器那么页面内容就上去了。 –

回答

3

尝试类似下面,

<Layout> 
    <Route path="/archive/:aid?" component={Archive} /> 
    <Route path="/featured" component={Featured} /> 
    <Route path="/setting" component={Setting} /> 
</Layout> 
+0

'margin-top'将除navbar外的整个页面移动到导航下60px的位置。我的问题是'Footer'在'Component archive like archive'之前。我希望组件在布局内呈现。 – ALH

+0

行了。您可以将所有页面放在Layout中,如 //您的页面在此处生成。 –

+0

我有'你不应该使用<路由组件>和<路由的孩子>在相同的路线的错误; <路线儿童>将被忽略' – ALH