反应路由 - 渲染错误(单页应用程序)

时间:2017-05-16 08:28:12

标签: reactjs react-router react-router-dom

您好我正在学习反应并尝试创建SPA,但内部页面不会呈现。

只有呈现,它才会呈现内部组件,如&

enter image description here

无法找到确切的问题

我尝试了多种路径路径组合,如

<Route path="/about" component={About}/>
<Route path="about" component={About}/>

我的完整代码

import React, { Component } from 'react';
import {render} from 'react-dom';
// import { Router, Route, Link } from 'react-router';
import { BrowserRouter as Router, Route, Link, Switch} from 'react-router-dom'; 
import About from './About';
import Home from './Home';
import Repos from './Repos';
import AboutChild from './AboutChild';

class App extends Component {
    constructor() {
        super(...arguments);
    }
    render(){
        return (
            <div>
                <menu>
                    <ul>
                        <li><Link to="/">Home</Link></li>
                        <li><Link to="/about">About</Link></li>
                        <li><Link to="/repos">Repos</Link></li>
                    </ul>
                </menu>
            </div>            
        );
    }
}


render((<Router>
            <App>
                <Route exact path="/" component={Home} />
                <Route path="/about" component={About}/>
                <Route path="/repos" component={Repos}/>
            </App>
</Router>), document.getElementById('root'));

package.json的组件版本

"dependencies": {
    "react": "^15.0.0",
    "react-dom": "^15.0.0",
    "react-router": "^4.1.1",
    "react-router-dom": "^4.1.1"
  }

2 个答案:

答案 0 :(得分:1)

在渲染中的App组件中,您需要返回子道具。试试这个

  render(){
    return (
        <div>
            <menu>
                <ul>
                    <li><Link to="/">Home</Link></li>
                    <li><Link to="/about">About</Link></li>
                    <li><Link to="/repos">Repos</Link></li>
                </ul>
            </menu>
            {this.props.children}
        </div>            
    );
}

您也可以删除构造函数,因为在此示例中不使用它。

答案 1 :(得分:0)

修复你的构造函数:

constructor(props) {
    super(props);
}