Javascript 在路由组件内部使用privateroute时,url中的id显示为未定义

Javascript 在路由组件内部使用privateroute时,url中的id显示为未定义,javascript,reactjs,react-router,react-router-v4,Javascript,Reactjs,React Router,React Router V4,我需要在公共和私有模式下显示与指南相关的组件。我的意思是指南列表,创建指南,指南细节应该可以被任何人查看,但编辑指南页面应该只能被认证的指南访问。为此,我尝试了以下方法 主要成分 App.js <Route path="/guides" component={Guides} /> 使用时,我无法获取指南ID。它给了我不明确的答案。但是如果我在App.js中使用了,那么它就可以完美地工作。但我不希望整个指南部分是私有的,只有编辑部分、支付部分应该是私有的 这是我的私人路线代码 con

我需要在公共和私有模式下显示与指南相关的组件。我的意思是指南列表,创建指南,指南细节应该可以被任何人查看,但编辑指南页面应该只能被认证的指南访问。为此,我尝试了以下方法

主要成分

App.js

<Route path="/guides" component={Guides} />
使用
时,我无法获取
指南ID
。它给了我不明确的答案。但是如果我在App.js中使用了
,那么它就可以完美地工作。但我不希望整个指南部分是私有的,只有编辑部分、支付部分应该是私有的

这是我的私人路线代码

const PrivateRoute = ({ component: Component, render, ...rest }) => {
  const renderContent = props => {
    console.log("PROPS", props, rest);
    if (!isLogin()) {
      return (
        <Redirect
          to={{
            pathname: "/auth/login",
            state: { from: props.location }
          }}
        />
      );
    }

    return typeof render === "function" ? (
      render(props)
    ) : (
      <Component {...props} {...rest} />
    );
  };
  return <Route render={renderContent} />;
};

PrivateRoute.propTypes = {
  component: PropTypes.oneOfType([PropTypes.func, PropTypes.element])
    .isRequired,
  location: PropTypes.object,
  render: PropTypes.func
};

export default PrivateRoute;
const PrivateRoute=({component:component,render,…rest})=>{
const renderContent=props=>{
控制台日志(“道具”,道具,休息);
如果(!isLogin()){
返回(
);
}
返回typeof render==“函数”(
渲染(道具)
) : (
);
};
返回;
};
PrivateRoute.propTypes={
组件:PropTypes.oneOfType([PropTypes.func,PropTypes.element])
.要求,
位置:PropTypes.object,
渲染:PropTypes.func
};
导出默认私有路由;

有人能帮我吗?

GuideForm
中,您可以使用

props.computedMatch.params.guideId
或者改变女贞路返回,如下图所示。(通过
…rest
返回路线)



为什么在使用PrivateRoute时需要使用computedMatch?你能告诉我这一点吗?因为你没有通过
…rest
到最后一条路线。祝你好运。
const PrivateRoute = ({ component: Component, render, ...rest }) => {
  const renderContent = props => {
    console.log("PROPS", props, rest);
    if (!isLogin()) {
      return (
        <Redirect
          to={{
            pathname: "/auth/login",
            state: { from: props.location }
          }}
        />
      );
    }

    return typeof render === "function" ? (
      render(props)
    ) : (
      <Component {...props} {...rest} />
    );
  };
  return <Route render={renderContent} />;
};

PrivateRoute.propTypes = {
  component: PropTypes.oneOfType([PropTypes.func, PropTypes.element])
    .isRequired,
  location: PropTypes.object,
  render: PropTypes.func
};

export default PrivateRoute;
props.computedMatch.params.guideId
<Route {...rest} render={renderContent} />