2017-03-03 73 views
0

在react-router将页面重定向到页面之前,我可以调用Meteor方法吗? 我想写一个登录用户关于他/她的页面浏览的历史记录。onBefore event react-router

我看到,在铁的路由器,我可以使用onBeforeAction: function() {}

我不能找到ReactTraining的反应路由器类似的事件处理程序。 我正在寻找他们的文档,https://github.com/ReactTraining/react-router/tree/1.0.x/docs

在流星服务器端,我使用简单的:json-routes。我也搜索他们的文档,并找不到与onBeforeAction事件处理程序相关的任何内容。

我正在使用反应路由器,所以我不能同时使用铁路由器。那么,有什么办法可以记录一个登录用户的页面浏览量在流星ReactJS

回答

1

尝试听浏览器历史记录browserHistory.listen

但首先你得有getCurrentLocation

+0

你能举一个简单的样本? – JMA

+0

browserHistory.listen(function(ev){console.log('listen',ev.pathname); }); {routes}

+0

我创建了一个配置文件。我编码到该文件的所有路由器组件。因此,我只需导入browserHistory模块,然后我将添加history = {browserHistory}到所有路由器组件? – JMA

1
来检测当前位置

是的!

通过:

  • 使用库*事件面向对象编程等

路由器helper.js

import {browserHistory} from 'react-router' ; 
import {fire} from 'mufa'; 

export function redirect(to) { 
    fire('onBeforeRedirect', to); 
    browserHistory.push(to); 
    fire('onAfterRedirect', to); 
} 
包封重定向逻辑在一个方法和
  • OtherFile-Subscribe-OnBefore.js

    import {on} from 'mufa'; 
    
    on('onBeforeRedirect', (to) => { 
        //Your code here will be running before redirection . 
    }); 
    

    组件的呼叫Redirect.js

    import {redirect} from 'router-helper'; 
    
    
    //... 
    
        redirect('/profile'); //<-- This call will trigger also onBefore listeners