【问题标题】:React.js Route not working properlyReact.js 路由无法正常工作
【发布时间】:2016-11-26 15:18:12
【问题描述】:

我的 / 路由没有正确呈现我的主要组件。

她是我的代码:

ReactDOM.render(
    <Router history={browserHistory}>
        <Route path="/" componenet={Main}>
            <IndexRoute component={Index}></IndexRoute>
            <Route path="portfolio" component={Portfolio}></Route>
        </Route>
    </Router>,
    document.getElementById('app')
);

这是我的主要组件:

export default class Main extends React.Component {     
    render() {
        console.log("asdfasfsaf");
        return(
        <div>
            <h1> This is Main Page </h1>
            {this.props.children}
        </div>
        )
    }

}

一旦我加载网站,它不会在控制台记录任何内容,也不会呈现 &lt;h1&gt; 这是主页标题。此外,如果我删除这个 Main 组件,它仍然会转到我的 IndexRoute 没有任何错误,这是我的索引组件。

【问题讨论】:

  • 您从哪个 URL 为您的网站提供服务。您是从localhost 还是localhost/my-site/public 之类的地方提供服务?
  • 只是 localhost:8080
  • 你的控制台有错误吗?
  • 控制台没有错误
  • 那么,/portfolio 正确加载而/ 没有? Portfoliorender 方法是什么样的?

标签: javascript reactjs react-router router


【解决方案1】:

试试这个:

ReactDOM.render((
    <Router history={browserHistory}>
        <Route path="/" componenet={Main}>
            <IndexRoute component={Index}></IndexRoute>
            <Route path="portfolio" component={Portfolio}></Route>
        </Route>
    </Router>,
  ), document.getElementById('app'));

【讨论】:

    猜你喜欢
    • 2014-12-05
    • 2018-01-17
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多