简体   繁体   English

Next.js 中的锚标记

[英]Anchor Tag in Next.js

I'm tryin got use an anchor tag in Next.js我正在尝试在 Next.js 中使用锚标记

I don't get any console errors when I set it up and click the link, but the page does not jump to the id tag.当我设置它并单击链接时,我没有收到任何控制台错误,但页面没有跳转到 id 标签。

This issue on github suggests that people need to figure out a lot of custom code to use anchors. github上的这个问题表明人们需要弄清楚很多自定义代码才能使用锚点。 That can't be right.那不可能是对的。

I have:我有:

const links = [
    { label: 'Solutions', href: '#solutions', id: 'solutions' },
    
  ]

 <NavLink.Desktop key={index} href={link.href} id={link.id}>
            {link.label}
          </NavLink.Desktop>

I get no errors, but the page does not jump to the label that has an id of 'solutions'.我没有收到任何错误,但页面没有跳转到 ID 为“解决方案”的标签。

Does anyone know how to solve this, or where to go for ideas on how - it can't be intented that complex custom code is required to use an anchor tag?有谁知道如何解决这个问题,或者在哪里可以找到关于如何解决的想法 - 使用锚标记需要复杂的自定义代码是不可能的?

As said by @juliomalves in the comments, you have to specify the id attribute in the element in which you have to navigate to.正如@juliomalves 在评论中所说,您必须在必须导航到的元素中指定id属性。 Not on the anchor tag.不在锚标签上。

The id for the anchor should be set on the element you want to link to, not on the link itself.锚点的 id 应该设置在您要链接到的元素上,而不是设置在链接本身上。

The below code works for me in Next.js -下面的代码在 Next.js 中对我有用 -

export default function Home() {
  return (
    <div>
      <a href="#secion">Click</a>

      <section
        style={{ marginTop: "1000px", marginBottom: "1000px" }}
        id="secion"
      >
        <h1>Test</h1>
      </section>
    </div>
  );
}

Your code should go like this -你的代码应该是这样的 -

const links = [{ label: 'Solutions', href: '#solutions', id: 'solutions' }]

<NavLink.Desktop 
     key={index} 
     href={link.href}
     // id={link.id} - This is wrong, as you're referring to the same element
>
   {link.label}
</NavLink.Desktop>

// Rather set the id attribute in a separate div/section element
<div id={link.id}>
   <h2>Solutions</h2>
</div>

maybe try也许试试

const links = [
{ label: 'Solutions', href: '#solutions', id: 'solutions' },

 ]

<NavLink.Desktop key={index} href={links[0].href} id={links[0].id}>
        {link.label}
</NavLink.Desktop>

since you only have 1 element in the links array, if you have multiple just map through the array因为您在 links 数组中只有 1 个元素,如果您有多个元素,则只需映射到该数组

It is possible to scroll to anchor programatically using Router.push:可以使用 Router.push 以编程方式滚动到锚点:

import { useRouter } from 'next/router'

const Foo = () => {
  const { push } = useRouter()

  const handleClick = () => {
    push("#blah")
  }

  return (
    <div>
      <button onClick={handleClick}>Scroll</button>
      <div>Foo</div>
      <div>Bar</div>
      <div id="blah">Blah</div>
    </div>
  )
}

Next.js recognises that you are passing something that is not a link to a new page and will concat it (in the example #blah ) to the end of the URL. Next.js 识别出您传递的不是指向新页面的链接的内容,并将它(在示例中#blah )连接到 URL 的末尾。

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

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