简体   繁体   English

React 路由器更改 url 但不呈现视图

[英]React router change the url but doesn't render the view

I know there are lots of similar questions here, but none of them solve my problem.我知道这里有很多类似的问题,但没有一个能解决我的问题。

Here's my App.js class:这是我的 App.js 类:

class App extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <div className="App">
                <Router>
                    <Switch>
                        <Route exact path="/" component={Header}/>
                        <Route path="/login" component={Login}/>
                        <Route path="/register" component={Register}/>
                        <Route path="/ranking" component={Ranking}/>
                        <Route path="/forum" component={Forum}/>
                    </Switch>
                </Router>
            </div>
        );
    }
}

And here's Header component, where I have menu with Links:这是 Header 组件,我在其中有带有链接的菜单:

class Header extends React.Component
{
    constructor(props) {
        super(props);
    }

    render() {
        return (
                <div className="header">
                    <div className="menu">
                        <Router>
                            <div className="option"><Link to="/login">Login</Link></div>
                            <div className="option"><Link to="/register">Register</Link></div>
                            <div className="option"><Link to="/ranking">Ranking</Link></div>
                            <div className="option"><Link to="/forum">Forum</Link></div>
                        </Router>
                    </div>
                </div>
        )
    }
}

My problem is: when I click the Link in Header section, URL in the browser changes, but is doesn't render component (view changes only after refreshing the page or write URL manually).我的问题是:当我单击页眉部分中的链接时,浏览器中的 URL 会发生变化,但不会呈现组件(仅在刷新页面或手动写入 URL 后查看更改)。 In brief - Links just doesn't work.简而言之 - 链接不起作用。 What I did wrong and how can I fix it?我做错了什么,我该如何解决?

Header组件中删除Router

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM