Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用react router dom和BrowserRouter在组件外部导航_Javascript_Reactjs - Fatal编程技术网

Javascript 如何使用react router dom和BrowserRouter在组件外部导航

Javascript 如何使用react router dom和BrowserRouter在组件外部导航,javascript,reactjs,Javascript,Reactjs,我正在使用BrowserRouter,我想在组件外部导航。我可以使用路由器导航,但我可以使用BrowserRouter吗 import { createBrowserHistory } from 'history'; export const history = createBrowserHistory(); <Router history = {history}> <Switch> <Route

我正在使用BrowserRouter,我想在组件外部导航。我可以使用路由器导航,但我可以使用BrowserRouter吗

 import { createBrowserHistory } from 'history';
 export const history = createBrowserHistory();

 <Router history = {history}>
              <Switch>
                <Route
                  path="/"
                  component={Home}
                  exact={true} />
                <Route
                  path="/somePath"
                  component={SomeDisplay}
                  exact={true} />  
</Router>

为了从组件外部进行路由,您需要访问路由器组件使用的历史对象。由于BrowserRouter不允许您访问组件外部的历史记录对象,因此您不能像这样导航使用
withRouter
HOC来访问历史记录:
import { history } from 'path/to/RouterConfigurations';

export const func1 = () => {

    history.push({
        pathname: "/somePath"
      });
}