简体   繁体   English

如何使用不同的路线进行反应?

[英]How to use different routes with react?

Could anyone help me with this question? 有人可以帮我解决这个问题吗?

I'm a bit lost when using different routes containing subroutes. 使用包含子路由的不同路由时,我有点迷路。

I am trying to add routes with different paths. 我正在尝试添加具有不同路径的路线。

I would like to have a default route with all the features and an alternate Event route separately. 我想拥有一个包含所有功能的默认路由和一个单独的事件路由。

From now on I am grateful for the attention and thank you very much. 从现在开始,我感谢您的关注,并非常感谢您。

index.js index.js

Contains the two different routes. 包含两条不同的路线。 Layout and Event. 布局和事件。 The Route Layout contains another Routes component inside. 路线布局内部包含另一个路线组件。

const Root = (
        <BrowserRouter>
            <Route component={App}>
                <Route component={Layout}/>
            </Route>
            <Route path="/event/:id" component={Event}/>
        </BrowserRouter>
)
ReactDOM.render(Root , document.getElementById('root'));

App.js App.js

Component to be loaded. 要加载的组件。 Layout or Event 布局或事件

class App extends Component {
  render() {
      const { children } = this.props;
      return (
          <div>{children}</div>
    );
  }
}
export default App;

RouterLayout.js --> This route is a subroute that is inside the Layout RouterLayout.js->此路由是Layout内部的子路由

const Router = () => (
    <Switch>
        <Route exact path='/' component={Home}/>
        <Route path='/about' component={About}/>
    </Switch>
)

export default Router

Layout.js Layout.js

class Layout extends Component {
      render() {
          const { children } = this.props;
          return (
              <Content className="column is-8 content-page">
                                    <RouterLayout/>
                                    <div>{children}</div>
                                </Content>
        );
      }
    }
    export default Layout;

I was able to solve this way 我能够这样解决

 <BrowserRouter>
            <div>
                <Route exact path="/page/*" component={Layout}/>
                <Route exact path="/event/:id" component={EmptyLayout}/>
            </div>
        </BrowserRouter>

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

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