链接组件在 Next.js 版本 13.4 中不起作用。

huangapple go评论53阅读模式
英文:

Link component is not working Next js v 13.4

问题

以下是您要翻译的代码部分:

import { Cursor, useTypewriter } from 'react-simple-typewriter';
import Image from 'next/image';
import React from 'react';
import BackgroundCircles from './BackgroundCircles';
import imageofme from '../public/imageofme.png';
import Link from 'next/link';

type Props = {}

export default function Hero({}: Props) {
  const [text,count] = useTypewriter({
    words: ['Hello World', 'Welcome to my website', 'I am a web developer'],
    loop: true,
    delaySpeed: 2000,
  })

  return (
    <div className='h-screen flex flex-col space-y-8 items-center justify-center
    text-center overflow-hidden'>
      <BackgroundCircles/>
      <Image className='relative rounded-fill h32 w-32 mx-auto object-cover'
      src={imageofme} 
      alt='image of me' />
      <div className='z-20'>
        <h2 className='text-sm uppercase text-gray-500 pb-2 tracking-[15px]'>
        Junior Software Developer
        </h2>
      <h1 className='text-5xl lg:text-6xl font-semibold px-10'>
      <span className='mr-3'>{text}</span>
      <Cursor cursorColor='black'/>
      </h1>
      <div className='pt-5'>
        <Link href='#about'>
        <button className='heroButton'>About</button>
        </Link>
        <Link href='#experience'>
        <button className='heroButton'>Experience</button>
        </Link>
        <Link href='#skills'>
        <button className='heroButton'>Skills</button>
        </Link>
        <Link href='#projects'>
        <button className='heroButton'>Projects</button>
        </Link>
      </div>
      </div>
    </div>
  )
}
'use client'
import About from "@/components/About";
import Experience from "@/components/Experience";
import Header from "@/components/Header";
import Hero from "@/components/Hero";

export default function Home() {
  return (
    <div className='bg-[rgb(55,64,104)] text-white h-screen snap-y snap-mandatory 
    overflow-scroll z-0'>
      <Header/>
      <section id='hero' className='snap-center'>
        <Hero/>
      </section>
      <section id='about' className='snap-center'>
        <About/>
      </section>
      {/*experience */}
      <section id='experience' className="snap-center">
        <Experience/>
      </section>
      {/*skills*/}
      {/*projects */}
      {/*Contact Me */}
    </div>
  )
}

希望这能帮助您解决问题。如果您需要进一步的帮助,请随时告诉我。

英文:

When I click on the buttons About, Experience and so forth. It is not directing me to the page but if I type the endpoint for that page it works so http://localhost:3000/#about so why is my button not taking me there when the section exists. HERE is the code.

import { Cursor, useTypewriter } from &#39;react-simple-typewriter&#39;;
import Image from &#39;next/image&#39;;
import React from &#39;react&#39;
import BackgroundCircles from &#39;./BackgroundCircles&#39;;
import imageofme from &#39;../public/imageofme.png&#39;;
import Link from &#39;next/link&#39;;
type Props = {}
export default function Hero({}: Props) {
const [text,count] = useTypewriter({
words: [&#39;Hello World&#39;, &#39;Welcome to my website&#39;, &#39;I am a web developer&#39;],
loop: true,
delaySpeed: 2000,
})
return (
&lt;div className=&#39;h-screen flex flex-col space-y-8 items-center justify-center
text-center overflow-hidden&#39;&gt;
&lt;BackgroundCircles/&gt;
&lt;Image className=&#39;relative rounded-fill h32 w-32 mx-auto object-cover&#39;
src={imageofme} 
alt=&#39;image of me&#39; /&gt;
&lt;div className=&#39;z-20&#39;&gt;
&lt;h2 className=&#39;text-sm uppercase text-gray-500 pb-2 tracking-[15px]&#39;&gt;
Junior Software Developer
&lt;/h2&gt;
&lt;h1 className=&#39;text-5xl lg:text-6xl font-semibold px-10&#39;&gt;
&lt;span className=&#39;mr-3&#39;&gt;{text}&lt;/span&gt;
&lt;Cursor cursorColor=&#39;black&#39;/&gt;
&lt;/h1&gt;
&lt;div className=&#39;pt-5&#39;&gt;
&lt;Link href=&#39;#about&#39;&gt;
&lt;button className=&#39;heroButton&#39;&gt;About&lt;/button&gt;
&lt;/Link&gt;
&lt;Link href=&#39;#experience&#39;&gt;
&lt;button className=&#39;heroButton&#39;&gt;Experience&lt;/button&gt;
&lt;/Link&gt;
&lt;Link href=&#39;#skills&#39;&gt;
&lt;button className=&#39;heroButton&#39;&gt;Skills&lt;/button&gt;
&lt;/Link&gt;
&lt;Link href=&#39;#projects&#39;&gt;
&lt;button className=&#39;heroButton&#39;&gt;Projects&lt;/button&gt;
&lt;/Link&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
)
}
Page.tsx 
&#39;use client&#39;
import About from &quot;@/components/About&quot;;
import Experience from &quot;@/components/Experience&quot;;
import Header from &quot;@/components/Header&quot;;
import Hero from &quot;@/components/Hero&quot;;
export default function Home() {
return (
&lt; div className=&#39;bg-[rgb(55,64,104)] text-white h-screen snap-y snap-mandatory 
overflow-scroll z-0&#39; &gt;
&lt;Header/&gt;
&lt;section id=&#39;hero&#39; className=&#39;snap-center&#39;&gt;
&lt;Hero/&gt;
&lt;/section&gt;
&lt;section id=&#39;about&#39; className=&#39;snap-center&#39;&gt;
&lt;About/&gt;
&lt;/section&gt;
{/*experience */}
&lt;section id=&#39;experience&#39; className=&quot;snap-center&quot;&gt;
&lt;Experience/&gt;
&lt;/section&gt;
{/*skills*/}
{/*projects */}
{/*Contact Me */}
&lt;/div&gt;
)
}

I tried following the docs and checking my console with no errors there. Attempted the Next link that didnt work , changed the paths so that they are / instead of #

答案1

得分: 1

你应该使用原生的HTML锚点,像这样:

<a href="#about">Text</a>

记住,next/link 只在 Next.js 页面之间进行路由时才需要使用。

英文:

You should use native HTML anchor like that:

&lt;a href=&quot;#about&quot;&gt;Text&lt;/a&gt;

Remember, next/link is only needed for routing between Next.js pages.

huangapple
  • 本文由 发表于 2023年6月5日 17:45:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定