简体   繁体   English

反应路由器链接不呈现组件但更改了 URL

[英]React Router Link Not Rendering the Component but changes the URL

import { BrowserRouter as Router, Route, Switch} from 'react-router-dom'
               <Router>
                <Fragment>
                    <Navbar />
                    <Switch>
                        <Route exact  path="/" component={Landing}/>
                        <Route   path="/developer/register/" component={RegisterDev} />
                        <Route   path="/developer/login/" component={LoginDev} />
                        <Route   path="/developer/dashboard/" component={Landing} />
                        <Route   path="/agency/register/" component={RegisterAgency} />
                        <Route   path="/agency/login/" component={LoginAgency} />

                        <Route component={NotFound} />
                    </Switch>
                </Fragment>
            </Router>

in My NavBar Component在我的导航栏组件中

//NavBar.js
<Link to="/"  Home > </Link>

The Problem is when I am in /developer/register or /developer/login component, if I click on Home Link, the URL Changes but not rendering the Landing component.问题是当我在 /developer/register 或 /developer/login 组件中时,如果我单击主页链接,URL 会更改但不呈现登陆组件。 All other components are working fine with Link.所有其他组件与 Link 一起工作正常。 Only problem with Link Home to /.唯一的问题是将主页链接到 /。 I tried withRouter, , Still the URL changes to / but the Landing component not rendering unless I refresh.我试过 withRouter, ,URL 仍然更改为 / 但除非我刷新,否则着陆组件不会呈现。

For now I am using href / to escape this problem but I want to understand why with Link its not rendering.现在我使用 href / 来逃避这个问题,但我想了解为什么 Link 不呈现。

Try adding exact in the route path.尝试在路由路径中添加精确。

import { BrowserRouter as Router, Route, Switch} from 'react-router-dom'
               <Router>
                <Fragment>
                    <Navbar />
                    <Switch>
                        <Route exact  path="/" component={Landing}/>
                        <Route exact   path="/developer/register/" component={RegisterDev} />
                        <Route exact   path="/developer/login/" component={LoginDev} />
                        <Route exact   path="/developer/dashboard/" component={Landing} />
                        <Route exact   path="/agency/register/" component={RegisterAgency} />
                        <Route exact   path="/agency/login/" component={LoginAgency} />

                        <Route component={NotFound} />
                    </Switch>
                </Fragment>
            </Router>
<Link to="/"  Home > </Link>

What syntax is this?这是什么语法?

Either you are trying to print Home as a label in which case it should be要么您尝试将 Home 打印为标签,在这种情况下它应该是

<Link to="/">Home</Link>

or you are trying to make this as part of your route in which case it should be或者您试图将其作为路线的一部分,在这种情况下应该是

<Link to="/Home"/> (Here there won't be a display text though) <Link to="/Home"/> (不过这里不会有显示文本)

Please check your syntax for Link where you are using Link and provide a sandbox if it still doesn't work.请检查您的语法链接,你正在使用的链接,并提供一个沙箱,如果它仍然无法正常工作。

I found the problem.我发现了问题。 I should have shared my Landing page component in the question.我应该在问题中分享我的着陆页组件。 I am using some jquery slider in my Landing component.我在 Landing 组件中使用了一些 jquery 滑块。 I suppose react routing will not load the page as everything here is virtual DOM.我想反应路由不会加载页面,因为这里的一切都是虚拟 DOM。 The reason it was working with href because it is reloading the entire home page.它使用 href 的原因是因为它正在重新加载整个主页。

With little research I found LoadJS Package通过很少的研究,我发现了LoadJS 包

Now I can load jquery slider scripts with load js and my Link to / is working fine without a problem.现在我可以使用 load js 加载 jquery 滑块脚本,并且我的链接到 / 工作正常,没有问题。 Thanks everyone who commented and responded .感谢所有评论和回复的人。 I should have posted Landing Page Component.我应该发布登陆页面组件。

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

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