【问题标题】:How to link 2 components in React and in a new page still have a navigation bar如何在 React 和新页面中链接 2 个组件仍然有一个导航栏
【发布时间】:2020-10-21 19:59:47
【问题描述】:

我已经为 React 建立了一个网站。我的代码在这里https://codesandbox.io/s/3d-tool-zjm5m?file=/src/components/modeling.jsx。我想要的是当用户点击建模选项卡中的“Go upload”按钮时,它会链接到一个新的 upload.jsx 页面,仍然有导航栏。

App.js 代码:

import React, { Component } from 'react'
import Navigation from './components/navigation';
import Header from './components/header';
import About from './components/about';
import Modeling from './components/modeling';
import Resources from './components/resources';
import Team from './components/Team';
import JsonData from './data/data.json';

export class App extends Component {
  state = {
    landingPageData: {},
  }
  getlandingPageData() {
    this.setState({landingPageData : JsonData})
  }

  componentDidMount() {
    this.getlandingPageData();
  }

  render() {
    return (
      <div>
        <Navigation />
        <Header data={this.state.landingPageData.Header} />
        <About data={this.state.landingPageData.About} />
        <Modeling data={this.state.landingPageData.Modeling} />
        <Resources data={this.state.landingPageData.Resources} /> 
        <Team data={this.state.landingPageData.Team} />
    
      </div>
    )
  }
}

export default App;

Modeling.jsx 代码:

import React, { Component } from "react";
import Upload from "./upload";

export class Modeling extends Component {
  uploadButton = (event) => {
    event.preventDefault();
    this.props.history.push({
      pathname: "/upload"
    });
  };
  render() {
    return (
      <div id="modeling">
        <div className="container">
          <div className="row">
            <div className="about-text">
              <h2>3D MODELING</h2>
            </div>
          </div>
          <div className=" wow fadeInUp slow">
            <div className="row pt-5 justify-content-center">
              <div className="col text-center">
                <h1>
                  <b>Pick what's right for you </b>
                </h1>
              </div>
            </div>
          </div>
          <div class="row p-5 p-md-0 pt-md-5 justify-content-around">
            <div
              class="col-md-5 mb-4 m-md-0 ml-md-5 modern-card card card-body shadow text-center wow fadeIn slow"
              data-wow-delay="0.2s"
            >
              <h2 class="mb-3 blue">
                <b>A 3D model from a 2D image.</b>
              </h2>

              <button
                type="button"
                class="btn btn-primary go-button"
                onclick={this.uploadButton}
              >
                Go upload
              </button>
            </div>

            <div
              class="col-md-5 mr-md-5 modern-card card card-body shadow text-center wow fadeIn slow"
              data-wow-delay="0.4s"
            >
              <h2 class="mb-3 blue">
                <b>A virtual tour from a 3D model.</b>
              </h2>

              <button
                type="button"
                class="btn btn-primary go-button"
                
              >
                Go!
              </button>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

export default Modeling;

Can someone stop by give me any suggestions? I have really appreciated it. Thanks!

【问题讨论】:

  • 请包含所有相关代码。您能否更新并分享您的路由/导航组件代码,以便我们可以看到您是如何渲染/匹配路由的?
  • NM,我现在看到您的应用没有使用任何导航/路由包。您似乎想要对新路线执行history.pushreact-router 是一个很好的解决方案(恕我直言)。 TL;DR,将您的应用程序包装在 Router 中,并将每个“页面”包装在 Route 中。使用传递的 history 路由属性命令式导航。如果您尝试这样做但遇到困难,我们可以提供帮助。
  • 你的意思是我把 App.js 中的所有东西都改成了路由器?像
  • codesandbox.io/s/3d-tool-zjm5m?file=/src/App.js 我只是对每个“页面”的路径进行了一些更改。但是,Modeling 选项卡中的“Go upload”按钮没有链接到 Upload.jsx。你能给我一些帮助吗?
  • @DrewReese 你能给我一些帮助吗?

标签: javascript reactjs navigation components


【解决方案1】:

您的导航栏包含指向“主页”部分的哈希链接。当您引入应用导航并想要多个页面时,您应该使用Link 组件而不是锚标签。您还需要将Navigation 组件放在路由上,以便它可以访问Router 上下文。

App.js

由于您已经将 整个 应用程序包装在 Router 中,因此您只需将 Navigation 和“主页”组件放在 Routes 中。将“主页”组件渲染为匿名组件,并记住将路由道具传递给每个组件。

<div>
  <Route component={Navigation} />
  <Switch>
    <Route path="/upload" component={Upload} />
    <Route
      render={(routeProps) => (
        <>
          <Header {...routeProps} data={this.state.landingPageData.Header} />
          <About {...routeProps} data={this.state.landingPageData.About} />
          <Modeling {...routeProps} data={this.state.landingPageData.Modeling} />
          <Resources {...routeProps} data={this.state.landingPageData.Resources} />
          <Team {...routeProps} data={this.state.landingPageData.Team} />
        </>
      )}
    />
  </Switch>
</div>

导航.jsx

为了保持哈希链接功能正常工作,您还需要从其他页面导入HashLink 组件。

import { HashLink } from "react-router-hash-link";

并将所有锚标签更改为HashLink 组件。

<HashLink className="navbar-brand page-scroll" to="/#page-top">
  3D RECONSTRUCTION TOOL
</HashLink>{" "}

...

<HashLink to="/#page-top" className="page-scroll">
  Home
</HashLink>
// ...etc...

建模.jsx

修复按钮的onClick 属性,它是onClick 而不是onclick。因为 route props 是从 App.js 传入的,所以 history prop 现在可用于命令式导航。

uploadButton = (event) => {
  event.preventDefault();
  this.props.history.push({
    pathname: "/upload"
  });
};

...

<button
  type="button"
  className="btn btn-primary go-button"
  onClick={this.uploadButton}
>
  Go upload
</button>

现在您可能会注意到,当导航到“/upload”上的新页面时,导航标题仍固定在窗口顶部,并且页面呈现在下方。这是从react-bootstrap 应用的样式。要解决此问题,您可以在正文中添加 padding-top 以将内容向下推送到标题下方。

body {
  padding-top: /* whatever the navigation header height is */;
}

您的标题是动态的,取决于视图宽度,因此您可能需要检查视图宽度并在 JS 中设置它。这是一个很有前途的解决方案:https://stackoverflow.com/a/38105035/8690857

【讨论】:

  • 感谢您的回答,对我帮助很大。还有一件事,你能帮我在主页上吗,为什么当我点击建模选项卡时,它看起来像是在下面呈现建模页面?
  • @Sunny 这是我回答的最后一点。这是因为 react-bootstrap CSS 和导航标题被固定在窗口顶部,页面内容也从页面顶部开始,但它在标题下方。您可以在正文中添加padding-top 以将内容下推,但由于标题高度是动态的,您可能需要在 JS 中执行此操作。我还链接到另一个解决这个问题的 SO question。
  • 非常感谢您的帮助!我真的很感激
猜你喜欢
  • 1970-01-01
  • 2018-02-18
  • 2019-09-08
  • 2016-09-14
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多