我尝试使用onEnter函数来要求对某些路由进行身份验证 . 我在“^ 1.0.0-rc4”版本中使用“react-router”,在“^ 1.17.0”版本中使用“history” . (升级很遗憾不是一种选择......)

onEnter函数调用很好,但“replaceState”函数不起作用 . Url在浏览器中更新(到/#/ login),但好的组件不是渲染 . 这不是我的“登录”组件谁是渲染,但我的“默认”组件...

代码 :

import React from 'react';
import { render } from 'react-dom';
import { createHistory, useBasename } from 'history';
import { Router, Route, IndexRoute } from 'react-router';
import "./common/styles/app-blue.less";
import Base from './components/layouts/Base';
import Default from './components/pages/dashboard/Default/Default';
import Home from './components/pages/dashboard/Home/Home';
import Login from './components/pages/dashboard/Login/Login';
import _404 from './components/pages/dashboard/Error/404';

const history = useBasename(createHistory)({
  basename: '/#'
});

var path = window.location.hash.substr(1);
if(path.length == 0)
  history.pushState(null, '/');
else
  history.pushState(null, path);

function handleHashChange() {
  var path = window.location.hash.substr(1);
  history.pushState(null, path);
}

window.addEventListener('hashchange', handleHashChange, false);

// Login or not login
function requireAuth(nextState, replaceState) {
    replaceState(null, '/login');
}

render(
    <Router history={history}>

        <Route path="/login" component={Login}/>

        <Route path="/" component={Base}>

            {/* Default route*/}
            <IndexRoute component={Default} />

            <Route path="dashboard" component={Home}/>

        </Route>

        {/* Not found handler */}
        <Route path="*" component={_404}/>

    </Router>,
    document.getElementById('app')
);