react-router IndexRedirect无效

打开路径/,不会跳转到/hot/now路由,indexRedirect的设置没起作用

import React, { Component } from 'react';
import ReactDom from 'react-dom';
import { Router, Route, IndexRoute, IndexRedirect, hashHistory } from 'react-router';
import { Provider } from 'react-redux';
import { syncHistoryWithStore } from 'react-router-redux';
import store from './redux/store';

const history = syncHistoryWithStore(hashHistory, store);

class App extends Component {
    render() {
        return (
            <div>
                {this.props.default}
            </div>
        );
    }
}

ReactDom.render(
    <Provider store={store}>
        <Router history={history}>
            <Route path="/" component={App}>
               <IndexRedirect to="/hot/now" />
               <Route path="p/:id"
                       getComponent={(location, callback) => {
                        require.ensure([], require => {
                            callback(null, require('./widget/article/article'))
                        }, 'article')
                } } />
                <Route path=":type/:cate"
                        getComponent={(location, callback) => {
                        require.ensure([], require => {
                            callback(null, require('./widget/trend/trend'))
                        }, 'trend')
                } } />
            </Route>
        </Router>
    </Provider>,
    document.getElementById('app')
);
阅读 6.9k
2 个回答

是不是前面的尖括号没写对,写成了中文的

试过你的代码,是可以跳转到 /#/hot/now 的,估计就是楼上说的尖括号问题。

当然,我是把其余 route 删掉后运行的:

ReactDom.render(
    <Provider store={store}>
        <Router history={hashHistory}>
            <Route path="/" component={App}>
               <IndexRedirect to="/hot/now" />
            </Route>
        </Router>
    </Provider>,
    root
);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题