简体   繁体   English

react中的嵌套路由不渲染嵌套组件

[英]Nested routing in react is not rendering nested component

Nested routing is not working.嵌套路由不起作用。 'Landing' And 'home' components showing properly but nested components are not rendering. 'Landing' 和 'home' 组件正确显示,但嵌套组件未呈现。

The layout component is directly imported in app.js there is no error in the console but nested components such as 'Feed' and 'Myprofile' are not rendering.布局组件直接导入 app.js 控制台没有错误,但嵌套组件如“Feed”和“Myprofile”没有呈现。

see the code看代码

import React from 'react';
import { Route, Switch, BrowserRouter as Router} from 'react-router-dom';
import Sidebar from '../componants/Sidebar'
import Navbar from '../componants/Navbar';
import PathRouter from '../Routers/PathRouters'
import Landing from '../pages/Landing';

const Home=()=>{
    
    return(
        <>
            <div className="container-fluid">
                <Navbar />

                <div className="rows row">
                    <div className='col-3'>
                        <Sidebar />
                    </div>
                    <div className="col-9 ">
                        <div className="content">                            
                            <Switch>
                                {PathRouter.map((prop, key) => {
                                    return (
                                        <Route
                                           exact path={prop.path}
                                            component={prop.component}
                                            key={key}
                                        />
                                    );
                                }
                                )}
                            </Switch>
                        </div>
                    </div>                
                </div>
            </div>
        </>
    )
}
const Layout = () => {      
    return (
        <>
            <Router>            
                <Switch>
                    <Route exact path='/' component={Landing} />
                    <Route exact path='/home' component={Home} />
                </Switch>            
            </Router>               
        </>
    )

}
export default Layout;

This is Pathrouter.js这是 Pathrouter.js

import Feed from '../pages/Feed';
import Answer from '../pages/Answer';
import Addquestion from '../componants/Addquestion';
import Myprofile from '../componants/Myprofile';

var PathRouter = [
    {
      path: '/home/feed',
      name: 'Feed',
      component: Feed
    },
    {
      path: '/home/answer/:q_id',
      name: 'answer',
      component: Answer
    },
    {
      path: '/home/addquestion',
      name: 'addquestion',
      component: Addquestion
    },
    {
      path: '/home/myprofile',
      name: 'myprofile',
      component: Myprofile
    }
]
export default PathRouter;

In the Layout component the line <Route exact path='/home' component={Home} /> is breaking your nested routes.Layout组件中, <Route exact path='/home' component={Home} />行破坏了您的嵌套路由。 For the nested route to render the parent route must also render, since '/home/feed' does not exactly match '/home' the parent route will not be rendered.对于要渲染的嵌套路由,父路由也必须渲染,因为'/home/feed''/home'不完全匹配,因此不会渲染父路由。 Remove exact from the '/home' route.'/home'路线中删除exact的。 Jacob Smit ——雅各布·斯密特

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

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