javascript - React JS 路由无法正常工作

标签 javascript reactjs routes react-router

我的/路线无法正确呈现我的主要组件。

这是我的代码:

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>
        )
    }

}

一旦我加载这个网站,它就会转到我的Index成分。但是它不会渲染 <h1> This is Main Page </h1>Main.js 。然后我彻底删除了Main.js中的所有代码并重新加载,发现它仍然运行,没有任何错误。有谁知道这个问题的解决方案吗?

最佳答案

您这里有一个拼写错误 <Route path="/" componenet={Main}>它的组件

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

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

关于javascript - React JS 路由无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38599276/

相关文章:

javascript - 显示 JSON 数据的下载进度百分比

javascript - 如何使用 JavaScript/jQuery 从一个链接打开两个页面?

node.js - 如何在reactjs中显示svg图像

ruby-on-rails - 如何在一个 Controller 中创建两个更新操作

ruby-on-rails-3 - 如何在URL中重命名REST路由?

javascript - 原型(prototype)取决于全局变量(如果该变量发生变化怎么办)

javascript - 如何禁止使用 '!!' 的警告(不是不是)?

javascript - webpack 拆分和导入模块

javascript - 如何修复 NextJS 中的暗模式背景颜色闪烁?

.htaccess - Yii 自定义 url 路由