【问题标题】:Confirming Navigation with React Router without Mixin for es6 Classes使用 React Router 确认导航而不使用 es6 类的 Mixin
【发布时间】:2016-03-20 17:09:35
【问题描述】:

使用 React Router 有没有一种方法可以在不使用 mixin 的情况下使用确认离开路由?我们使用的是 es6 类,不能使用 mixin。

这就是我想要完成的: https://github.com/rackt/react-router/blob/latest/docs/guides/advanced/ConfirmingNavigation.md

【问题讨论】:

    标签: reactjs react-router


    【解决方案1】:

    您将需要使用History 模块,然后使用listenBefore():

    history.listenBefore(function (location) {
      if (input.value !== '') {
        return 'Are you sure you want to leave this page?'
      }
    })
    

    查看Confirming Navigation 文档。

    【讨论】:

    • 有没有办法删除listenBefore并清除它?
    • 要移除监听器,你只需要保存返回的内容并执行它。 this.unlisten = browserHistory.listenBefore(function() { if (!window.confirm('Leaving?')) { return false; } }); this.unlisten();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 2017-12-20
    • 2020-05-18
    • 2022-01-01
    • 2021-04-24
    • 2021-05-19
    相关资源
    最近更新 更多