【问题标题】:Is there a way so that I can access all the items in the browser history or atleast top item of history有没有办法让我可以访问浏览器历史记录中的所有项目或至少历史的顶部项目
【发布时间】:2019-07-03 04:25:38
【问题描述】:

我有一个场景,我必须在提交表单时采取一些行动。

我是从两条途径得出这种特殊形式的。比方说 /first-route 和 /second-route

现在我的目标很简单,如果我来自 /first-route,我必须在提交表单时返回 /first-route。但如果我来自 /second-route 或其他任何地方,我必须去 /third-route。

有没有办法访问 react-router 历史对象中的最后一项,以便我可以有条件地进行此路由。

history.goBack() 函数将我带回最后一条路线,但我想要的是在返回之前先检查这条路线

如果路线是 /first-route 我想做history.push('/first-route')。在其他情况下,我必须这样做history.push('/third-route')

对于更多上下文,我有两个独立的反应应用程序,必须在它们之间进行通信。

我有两个应用程序嵌入在第三个应用程序中。我想让父应用独立于这种通信。

【问题讨论】:

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


    【解决方案1】:

    属性 每个历史对象都有以下属性:

    history.length - The number of entries in the history stack history.location - The current location (see below) history.action - The current navigation action (see below)

    此外,createMemoryHistory 提供了 history.index 和 history.entries 属性,可让您检查历史堆栈。

    https://www.npmjs.com/package/history

    【讨论】:

      猜你喜欢
      • 2021-12-02
      • 2015-01-31
      • 2012-10-07
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 2012-04-25
      • 1970-01-01
      • 2021-07-07
      相关资源
      最近更新 更多