Javascript 反应:“;历史";道具在专用路线中不可用
我在我的应用程序中使用React 16、React路由器dom 4和Mobx 我有一条私人路线的代码:Javascript 反应:“;历史";道具在专用路线中不可用,javascript,reactjs,react-router,Javascript,Reactjs,React Router,我在我的应用程序中使用React 16、React路由器dom 4和Mobx 我有一条私人路线的代码: export default (props) => { console.log('props from private',props)//Here i can see that the component doesn't contain the "history" prop. const Component = props.component; cons
export default (props) => {
console.log('props from private',props)//Here i can see that the component doesn't contain the "history" prop.
const Component = props.component;
const match = props.computedMatch
if (isValidated()) {
return (
<div>
<div><Component {...props} match={match} /></div>
</div>
)
} else {
return <Redirect to="/login" />
}
};
导出默认值(道具)=>{
log('props from private',props)//在这里我可以看到组件不包含“history”属性。
const Component=props.Component;
常量匹配=props.computedMatch
if(isValidated()){
返回(
)
}否则{
返回
}
};
这是路由设置:
export const history = createHistory();
const AppRouter = () => (
<Router history={history}>
<Switch>
<PrivateRoute path="/" component={Chat} exact={true} />
<Route path="/login" component={Login} />
</Switch>
</Router>
);
export const history=createHistory();
常量批准器=()=>(
);
由于某些原因,历史道具不存在于私有路由中,因此我无法使用this.props.history.push函数以编程方式重定向。不过道具确实会通过“正常”路线
我的代码有什么问题?请使用以下命令:
import {withRouter} from 'react-router-dom';
用withRouter包装组件
withRouter(component_name)
使用方法如下:
import {withRouter} from 'react-router-dom';
用withRouter包装组件
withRouter(component_name)