首页 > 解决方案 > HashRouter,基根“/”

问题描述

我正在使用 HashRouter 路由我的 SPA,问题来自这样一个事实,即我希望在返回主页选项卡时刷新页面,我的 Header 组件中的基本根“/”

<LinkContainer to="/" onClick={window.location.reload}>
        <Navbar.Brand>
          <img src={logo} alt="" />
        </Navbar.Brand>
      </LinkContainer>

在我实现 HashRouter 之前,这是可行的,因为我在 home 组件中有一个脚本来呈现一系列文本,该文本并不总是呈现,在 hashrouter 之前,如果您通过导航栏品牌返回主页,则该文本不会呈现,我已修复通过导致页面重新加载来解决该问题。(下面序列文本的代码)

window.onload = () => {
    new SequenceRunner({
      // Selector targets the html container, in this case a <p> with class (sequence-runner)
      selector: ".sequence-runner",
      // Content to be displayed.
      content: ["Simple", "Reliable", "Affordable"],
      // Time
      delay: 2500,
    }).start();
  };

但是现在我开始使用哈希路由器,它以“localhost:3000/#/”为基础,我相信它无法识别链接容器请求。

这也是我的 app.js

<HashRouter>
      <main>
        <Container fluid>
          <Header />
          <Route path="/contact" component={ContactUs} />
          <Route path="/terms-of-use" component={Terms} />
          <Route path="/privacy-policy" component={PrivacyPolicy} />
          <Route path="/support" component={Support} />
          <Route path="/signup" component={SignUp} />
          <Route path="/about" component={About} />
          <Route path="/pricing" component={Pricing} />
          <Route path="/features" component={TCFeatures} />
          <Route path="/" component={Home} exact />
          <Footer />
        </Container>
      </main>
    </HashRouter>

标签: javascriptreactjsreact-router

解决方案


推荐阅读