简体   繁体   English

路由在Reactjs + ES6中不起作用

[英]Routing is not working in Reactjs + ES6

When I am clicking on the link "cake" I am getting the error the path is not matched or server is not able to find. 当我单击“蛋糕”链接时,出现错误,指出路径不匹配或服务器找不到。

Here is my code for 3 files - Router, Navigation and detail components 这是我的3个文件的代码-路由器,导航和详细信息组件

Routers.js- component handling the routing Routers.js-处理路由的组件


import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route , HashHistory , IndexRoute , useRouterHistory} from 'react-router';
import History from 'history';
import {CreateHashHistory} from 'history';
import createBrowserHistory from 'history/lib/createBrowserHistory';
import { browserHistory } from 'react-router';

import Base from './Base.jsx';
import ListDetail from './ListDetail.jsx';

let Routes = 

        <Router history={browserHistory}>
            <Route path="/" component={Base} >
                <Route path="/cake" component= {ListDetail}></Route>

            </Route>
        </Router>

 export default Routes;

Navigation components - handling the navigaton links 导航组件-处理导航链接

import React from 'react';
import ReactDOM from 'react-dom';
import { Link } from 'react-router';


class ContentList extends React.Component {
    render(){
        return(
            <div id="innercontent">
            <h2>What you love?</h2>
            <ul >
                <Link to={'/cake'}>Cakes</Link>
                <Link to={'/icecream'}>icecream</Link>
                <Link to={'/browin'}>browin</Link>
            </ul>
            </div>
        )
    }
}

export default ContentList;

Detail Component - Detail page to display 详细信息组件-要显示的详细信息页面

import React from 'react';

import ReactDOM from 'react-dom';

class ListDetail extends React.Component {
    render(){
        return(
            <div>
            <h1>hi Details</h1>


            </div>
        )
    }
}


export default ListDetail;

Base.JSX Base.JSX

import React from 'react';
import ReactDOM from 'react-dom';
import Header from './header.jsx';
import Footer from './footer.jsx';
import ContentList from './contentList.jsx';
import FormElement from './form.jsx';
import ListDetail from './ListDetail.jsx';

class Base extends React.Component {
    render(){
        return(
            <div>
            <Header name="My Recipe Book"/>
            <section id="content">
            <FormElement />
             <ContentList />
            </section>
            <Footer />
            <ListDetail />
            </div>
        )
    }
}

export default Base; 导出默认Base

Main.jsx Main.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import Routes from './router.jsx';

ReactDOM.render(Routes, document.getElementById('app'));

Thanks for the help!! 谢谢您的帮助!!

You don't need the / in /cake , it's already under / . 您不需要/cake中的/cake ,它已经在/下。 Do it like this: 像这样做:

<Route path="cake" component={ListDetail} />

The render function of your base.jsx file should be something like this : 您的base.jsx文件的render函数应该是这样的:

render(){
    return(
        <div>
        <Header name="My Recipe Book"/>
        <section id="content">
        <FormElement />
        <ContentList />
         {this.props.children}
        </section>
        <Footer />
        </div>
    )
}

Route should be 路线应为

<Route path="cake" component= {ListDetail} />

And the link : 和链接:

<Link to="cake">Cakes</Link>

You need to render the routes somewhere. 您需要在某处渲染路线。

...A single component to be rendered when the route matches the URL. ...当路线与URL匹配时要呈现的单个组件。 It can be rendered by the parent route component with this.props.children. 父路线组件可以使用this.props.children来呈现它。 Read this. 读这个。

const routes = (
  <Route path="/" component={App}>
    <Route path="groups" component={Groups} />
    <Route path="users" component={Users} />
  </Route>
)

class App extends React.Component {
  render () {
    return (
      <div>
        {/* this will be either <Users> or <Groups> */}
        {this.props.children}
      </div>
    )
  }
}

Try the following, not sure if it would help. 请尝试以下操作,不确定是否有帮助。 1. Use 1.使用

   var ContentList = React.createClass({}) 

instead of 代替

    class ContentList extends React.Component.

2. remove the / before 'cake' in your route path. 2.删除路径中“ / cake”前的/。

  1. try using 尝试使用

      var Routes = React.createClass({ render: function() { return ( <div> <Router history={browserHistory}> 

and your updated code instead of let 和您更新的代码,而不是让

hope it helps 希望能帮助到你

Does it work with hashHistory ? 它可以与hashHistory一起使用吗?

To use browserHistory, you need to set up server side rendering. 要使用browserHistory,您需要设置服务器端渲染。

Here is the tutorial (especially lesson 11 and 13) which helped me understand and set up react-router using browserHistory and server side rendering: https://github.com/reactjs/react-router-tutorial/tree/master/lessons/ 这是教程(尤其是第11和13课),它帮助我使用browserHistory和服务器端渲染来理解和设置react-router: https : //github.com/reactjs/react-router-tutorial/tree/master/lessons/

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM