【问题标题】:How do I get out of MemoryRouter nested inside the BrowserRouter?如何摆脱嵌套在 BrowserRouter 中的 MemoryRouter?
【发布时间】:2020-09-07 09:01:31
【问题描述】:

问题是我需要在不更改 URL 的情况下通过 3 个组件进行路由,但也有一些组件会更改路由。

所以我将 MemoryRouter 嵌套在 BrowserRouter 中,但是当我进入 MemoryRouter 时,没有出来。

我试过这个:-

   const history=createBrowserHistory();
    window.browserHistory=history;
    <BrowserRouter history={history}>   
    <Switch>
      <Route path="/quotes" component={QuotePage} />
      <Route exact path="/" component={GreetingPage} />
    </Switch>
    </BrowserRouter>

在 MemoryRouter 里面我有这样的东西。

<MemoryRouter>
  <Switch>
    <Route exact path="/" component={Form1} />
    <Route path="/form2" component={Form2} />
  </Switch>
<MemoryRouter>

在表格 2 中,我有一个调用函数

window.browserHistory.push("/quotes");

现在“/quotes”在链接中可见,但组件未加载。

【问题讨论】:

    标签: reactjs react-router nested-routes


    【解决方案1】:

    使用 html a 标签代替 react-router 链接组件,以脱离 MemoryRouter。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-15
      • 2020-08-04
      相关资源
      最近更新 更多