【问题标题】:React-router v4. How to navigate between routes?反应路由器 v4。如何在路线之间导航?
【发布时间】:2017-11-02 14:33:24
【问题描述】:

路由不起作用。

它只在 / 上渲染第一个组件 Cloud,但是当我在其中添加其他路由时不起作用

import React from 'react';
import {render} from 'react-dom';
import { BrowserRouter as Router, Route } from 'react-router-dom'
import createBrowserHistory from 'history/createBrowserHistory';

// import components
import Cloud from './components/Cloud';
import Dashboard from './components/Dashboard';
import Contacts from './components/Contacts';
import Account from './components/Account';
import Login from './components/Login';
import Sidebar from './components/Sidebar';
import Header from './components/Header';
import Footer from './components/Footer';

const history = createBrowserHistory();

const App = (props) => {
return (
    <Router history={history}>
        <section>
            <Sidebar/>
            <div className="main-content">
                <Header/>
                <div id="page-wrapper">
                    <Route exact path="/" component={Cloud}/>
                    <Route path="/dashboard" component={Dashboard}/>
                    <Route path="/contacts" component={Contacts}/>
                    <Route path="/account" component={Account}/>
                </div>
            </div>
            <Footer/>
        </section>
    </Router>
    );
};

render(<App/>, document.getElementById('root'));

我做错了什么? screenshotscreenshot2

如果用户 cookie 过期,如何在 /home 和 /login 路由之间跳转?

【问题讨论】:

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


    【解决方案1】:

    只需在路径前面放一个/,即

    <Route exact path="/" component={Cloud}/>
    <Route path="/dashboard" component={Dashboard}/>
    <Route path="/contacts" component={Contacts}/>
    <Route path="/account" component={Account}/>
    

    【讨论】:

    • 不会有帮助的。我只是忘了写这个/在这里
    猜你喜欢
    • 2018-07-28
    • 1970-01-01
    • 2017-09-27
    • 2021-03-29
    • 2016-04-10
    • 2017-12-06
    • 2017-12-06
    • 2017-10-21
    • 2020-10-27
    相关资源
    最近更新 更多