简体   繁体   English

React router 4如何嵌套路由/ admin和/

[英]React router 4 how to nested routes /admin and /

I have problem with nested routing. 我有嵌套路由的问题。 On the normal site I have other urls than on the / admin page and i have different design and html. 在普通网站上我有其他网址而不是/ admin页面,我有不同的设计和HTML。

I prepared this sample routing but after the page refreshes, the page gets white without any error. 我准备了这个示例路由,但在页面刷新后,页面变为白色,没有任何错误。

Can I ask for a consultation what did I do wrong? 我可以要求咨询我做错了什么吗?

APP COMPONENT APP组件

class App extends Component {
render() {
    return (
        <BrowserRouter>
            <div className="container">
                <Route exact path="/" render={(props) => (
                    <Page {...props} data={data} />
                )} />
                <Route exact path="/admin" render={(props) => (
                    <Admin {...props} data={data} />
                )} />
            </div>
        </BrowserRouter>
    );
}

} }

PAGE COMPONENT PAGE组件

class Page extends React.Component {
render() {
    return (
        <BrowserRouter>
            <div>
                <Header />

                    <Route exact path="/" render={(props) => (
                        <Home {...props} videosJson={this.props.data} />
                    )} />

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

                    <Route exact path="/video" render={(props) => (
                        <VideoGallery {...props} videosJson={this.props.data} />
                    )} />

                    <Route path="/video/:id" render={(props) => (
                        <VideoPage {...props} videosJson={this.props.data} />
                    )} />

                    <Route exact path="/photo" render={(props) => (
                        <PhotoGallery {...props} videosJson={this.props.data} />
                    )} />

                    <Route path="/photo/:id" render={(props) => (
                        <PhotoPage {...props} videosJson={this.props.data} />
                    )} />

                    <Route path="/contact" component={ Contact } />

                <Footer />
            </div>
        </BrowserRouter>
    )
}

} }

ADMIN COMPONENT ADMIN COMPONENT

class Admin extends React.Component {
render() {
    return (
       <BrowserRouter>
            <div>
                    <Route exact path="/admin" render={(props) => (
                        <Dashboard {...props} />
                    )} />

            </div>
        </BrowserRouter>
    )
}

} }

Your React application which uses React-Router should only have one instance of a Router defined, as stated in the documentation: 使用React-Router的React应用程序应该只定义一个Router实例,如文档中所述:

The common low-level interface for all router components. 所有路由器组件的通用低级接口。 Typically apps will use one of the high-level routers instead 通常,应用程序将使用其中一个高级路由器

The error you are getting is because you are defining additional routers (in your case there are multiple instances of BrowserRouter) in your Page and Admin components. 您得到的错误是因为您在页面和管理组件中定义了其他路由器(在您的情况下,有多个BrowserRouter实例)。

Also some of your Routes are ambiguous eg 你的一些路线也是模棱两可的

<Route exact path="/" render={(props) => (
 <Page {...props} data={data} />
)} />

and: 和:

<Route exact path="/" render={(props) => (
 <Home {...props} videosJson={this.props.data} />
)} />

One Route says that root ('/') should navigate to the Page component, the other says that root should navigate to the Home component, hence there is a conflict. One Route表示root('/')应该导航到Page组件,另一个表示root应该导航到Home组件,因此存在冲突。 Make sure the routes are unique. 确保路线是唯一的。

I change my approach to this situation but dont work. 我改变了我对这种情况的态度但是没有用。 Url /admin load Header and Footer component although he should not and component Dashboard not load. Url / admin加载页眉和页脚组件虽然他不应该和组件仪表板不加载。

Any sugestion? 任何消化?

<BrowserRouter>
            <div className="container">

                    <Page>
                        <Header />

                            <Route exact path="/" render={(props) => (
                                <Home {...props} videosJson={data} />
                            )} />

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

                            <Route exact path="/video" render={(props) => (
                                <VideoGallery {...props} videosJson={data} />
                            )} />

                            <Route path="/video/:id" render={(props) => (
                                <VideoPage {...props} videosJson={data} />
                            )} />

                            <Route exact path="/photo" render={(props) => (
                                <PhotoGallery {...props} videosJson={data} />
                            )} />

                            <Route path="/photo/:id" render={(props) => (
                                <PhotoPage {...props} videosJson={data} />
                            )} />

                            <Route path="/contact" component={ Contact } />

                        <Footer />
                    </Page>

                    <Admin>
                        <Route exact path="/admin" render={(props) => (
                            <Dashboard />
                        )} />
                    </Admin>

            </div>
        </BrowserRouter>

Admin Component: 管理组件:

class Admin extends React.Component {
  render() {
      console.log("ADMIN:", this.props);
      return (
        <div className="row">
            <h1>ADMIN</h1>
            {this.props.children}
        </div>
      )
   }
}

Page Component: 页面组件:

class Page extends React.Component {
   render() {
      console.log("PAGE:", this.props);
      return (
         <div>
            {this.props.children}
        </div>
      )
   }
}

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

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