如何从索引页重定向到其他页面

时间:2017-06-02 10:22:07

标签: javascript reactjs react-router react-router-v4

如何通过从localhost加载页面来重定向:xxxx /#/到localhost:xxxx /#/ month?

我有这样的index.js代码,它调用App类:

ReactDOM.render((
    <HashRouter>
        <Route path="/" component={App}>
        </Route>
    </HashRouter>
), document.getElementById('root'));

App.js有静态信息,在组件View中将是动态信息:

export default class App extends React.Component {
  render() {
    return (
      <div>
        Hello App)
        <View/>
      </div>
    );
  }
}

View.js:

export default class View extends React.Component {
    render() {
        return(
                <div>
                    <p>this is View</p>
                    <ul>
                        <li><Link to="/month">Month</Link></li>
                        <li><Link to="/week">Week</Link></li>
                        <li><Link to="/day">Day</Link></li>
                        <li><Link to="/year">Year</Link></li>
                    </ul>
                    <Switch>
                        <Route exact path="/month" component={Month} />
                        <Route path="/week" component={Week} />
                        <Route path="/day" component={Day} />
                        <Route path="/year" component={Year} />
                    </Switch>
                </div>
        );
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用Redirect

<Route exact path={your_root_path} render={() => <Redirect to={month_path} />} />