在路由更改之前解析数据

Resolve data before route change

我来自Angular 1说实话,我知道一点angular 2,反应过来。我知道这两种架构,但我不愿意像大师一样制作快速应用程序。

我遇到了问题。在 angular 中很容易,在路由更改之前,我可以解析或不解析我的数据以更改路由,包括我的 controller/view。

如何通过反应来做到这一点?因此,一旦我的数据得到解决,就改变路线。我没有发现任何有趣的东西。

如果您使用 react-router 进行导航,那么您可以挂钩 onEnter 并推迟对 callback 的调用,直到您的数据得到解析。

https://github.com/ReactTraining/react-router/blob/v3/docs/API.md#onenternextstate-replace-callback

有几种方法,你可以试试setRouteLeaveHook:

class SomeRoute extends Component {
    componentDidMount() {
        this.props.router.setRouteLeaveHook(
            this.props.route,
            nextRoute => this.routerWillLeave(nextRoute)
        );
    }

    routerWillLeave(nextRoute: History.LocationDescriptor): boolean | void {
        /* ... *?
    }
}

其中 SomeRoute 是作为 component 属性 到 ReactRouter.Route 的任何组件:

<Router>
    <Route component={SomeRoute}/>
</Router>

https://github.com/ReactTraining/react-router/blob/master/docs/guides/ConfirmingNavigation.md