簡體   English   中英

如何在 React 和新頁面中鏈接 2 個組件仍然有導航欄

[英]How to link 2 components in React and in a new page still have a navigation bar

我已經建立了一個網站來響應。 我的代碼在這里https://codesandbox.io/s/3d-tool-zjm5m?file=/src/components/modeling.jsx 我想要的是當用戶點擊 Modeling 選項卡中的“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!

您的導航欄具有指向“主頁”部分的哈希鏈接。 當您引入應用導航並且想要多個頁面時,您應該使用Link組件而不是錨標記。 您還需要將Navigation組件放置在路由上,以便它可以訪問Router上下文。

應用程序.js

由於您已經將整個應用程序包裝在Router您只需將Navigation和“主頁”組件放在Route 將“主頁”組件渲染為匿名組件,並記住將路由道具傳遞給每個組件。

<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 功能正常工作,您還需要從其他頁面導入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 因為路由道具是從App.js ,所以history道具現在可用於命令式導航。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM