亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

GatsbyJS + React 中的錨鏈接

GatsbyJS + React 中的錨鏈接

繁星淼淼 2023-04-27 16:53:55
我已經在這個問題上停留了一段時間了。我在普通的 create-react-app 中創建項目后創建了一個 GatsbyJS 項目。一切正常,直到我對代碼進行了一些重大更改。這毀了我的導航 - 但不完全!徽標導航和頁腳中的鏈接仍然有效,但 NavBar 項目無效。我正在使用 gatsby 插件:'gatsby-plugin-anchor-links' ( https://www.gatsbyjs.com/plugins/gatsby-plugin-anchor-links/ )。這是我的 NavBarItem 代碼(組件):import React from "react"import "../../Bulma.css"import { Link } from "gatsby"function NavBarItem(props) {  return (    <Link      className={"pl-6 pr-6 has-text-black navbar-item " + props.class}      to={"/#" + props.location}    >      {props.text}    </Link>  )}export default NavBarItem這是我的 NavBar 組件:import React from "react"import NavBarItem from "./assets/NavBarItem"import "../Bulma.css"import "./NavBar.css"import { Link } from "gatsby"import logo from "../../static/dronarfoton_logo.png"class NavBar extends React.Component {  constructor(props) {    super(props)    this.state = {      isActive: true,    }  }  toggle() {    this.setState({ isActive: !this.state.isActive })  }  render() {    return (      <nav        className="navbar has-text-white has-background-grey-lighter has-navbar-fixed-top is-fixed-top"        role="navigation"        aria-label="main navigation"      >        <div className="navbar-brand">          <a href="#Top" className="navbar-item">            <img              alt="Logga som det st?r Dr?narFoton p?. Det visar en dr?nare och text."              src={logo}              width="112"              height="28"            />          </a>          <a            role="button"            className={              this.state.isActive                ? "navbar-burger burger"                : "is-active navbar-burger burger"            }            aria-label="menu"            aria-expanded={this.state.isActive ? "false" : "true"}            data-target="navbarBasicExample"            onClick={this.toggle.bind(this)}同樣,“導航欄品牌”鏈接有效,但導航欄項目無效。我的想法是它與它的呈現方式有關,但我無法弄清楚這是如何發生的以及為什么會發生。另一個有趣的部分是,如果我在我的瀏覽器中禁用 JAVASCRIPT,鏈接就可以工作如果有人知道為什么會這樣。請告訴我。謝謝//古斯塔夫
查看完整描述

1 回答

?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

prop?location您在組件中使用 a<NavBarItem>但最后,您呈現的是<Link>, 它不接受散列 (?#) 既不接受錨點行為。<Link>正如您在的 API 文檔(應用內導航)中所見:

既不能<Link>navigate不能用于帶有散列或查詢參數的路線導航。如果您需要這種行為,您應該使用錨標記或導入@reach/router包(Gatsby 已經依賴該包)以使用其導航功能。

如果你想在你的 中使用錨鏈接<NavBarItem>,你應該使用常規<a>gatsby-plugin-anchor-links正確使用:

?<AnchorLink
????to="/about#team"
????title="Check?out?our?team!"
????className="stripped"
????stripHash
??/>

請記住,使用常規<a>,您將失去 上的所有好處,@reach/routing并且您將完全刷新頁面。


查看完整回答
反對 回復 2023-04-27
  • 1 回答
  • 0 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號