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

How to redirect from index page to other page

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

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

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>
        );
    }
}

您可以使用 Redirect.

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