2017-03-03 70 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