反应路由器的嵌套路由。不呈现子路线

时间:2018-08-05 13:26:56

标签: reactjs react-router react-router-v4 react-router-dom

我有一个称为Dashboard的父组件,它像这样被渲染为Route:

const Routes = ({ authenticated }: { authenticated: boolean }) => (

    <Switch>
        <AuthenticatedRoute path="/home" exact={true} component={Dashboard} authenticated={authenticated} />
        <UnauthenticatedRoute path="/login" exact={true} component={Login} authenticated={authenticated} />
        <AuthenticatedRoute path="/onboarding" exact={true} component={Onboarding} authenticated={authenticated} />
        <AuthenticatedRoute path="/meets" exact={true} component={Meets} authenticated={authenticated} />
        <Route component={NotFound} />
    </Switch>

)

export default Routes

在Dashboard组件中,我还要呈现路线,该组件如下所示:

class Dashboard extends React.Component<IProps, {}> {
    public render() {
        return (
            <div>
                <Navigation />

                <Route exact={true} path="/home" component={Home} />
                <Route exact={true} path="/home/profile" component={Profile} />
                <Route exact={true} path="/home/messages" component={Messages} />

                HALLO
            </div>
        )
    }
}

我也尝试过将这些路由包装在Switch中,同样的问题。

为了我,我不知道为什么这不起作用。本地路由可以很好地呈现,但“个人档案”或“消息”则不能。当我尝试点击这些URL时,我会得到在第一个代码块中找到的NotFound路由组件。

路径已设置为'/ home / profile /和'/ home / messages',为什么当我点击这些URL时为什么不呈现它们?

我尝试在嵌套路线上嘲笑每个人的解决方案,但似乎无法解决我的问题。

1 个答案:

答案 0 :(得分:4)

因为您在顶级路线配置中使用了exact道具。放下它:

<AuthenticatedRoute path="/home" component={Dashboard} authenticated={authenticated} />

此外,exact就足够了,您不必像exact={true}那样使用它。

相关问题