import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import Reducer from './Reducer/';
import {HashRouter as Router, Route, Redirect} from 'react-router-dom';

import Login from './Components/login.jsx';
import User from './Components/User.jsx';








const store = createStore(Reducer, applyMiddleware(thunk));




ReactDOM.render(

    <Provider store={store}>
        <Router>
            <div>
                <Route path='/login' component={Login}/>
                <Route path='/user' component={User}/>
            </div>
        </Router>

    </Provider>


    ,document.getElementById('root')); 





当用户点击“ /” URL时,我希望他们位于登录组件中,但是如果我这样做:

<Route path='/' component={Login}/>

登录组件始终显示在每条不同的路线上。
我的问题是。在登录页面“ /”上,如何使它成为登录组件,但是如果我转到用户组件,则我不希望它出现

最佳答案

您可以执行<Route path='/' component={Login} exact />指定仅在完全/时渲染该组件,仅此而已。

关于javascript - 如何使用react-router-dom重定向,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46615211/

10-11 12:55