NUXT3 -NUXTLINK锚定不滚动单击或页面刷新

发布于 2025-02-13 11:27:22 字数 1572 浏览 0 评论 0原文

我只是尝试将页面滚动到NUXT3中的一个锚点,而我所能做的任何事情都无法正常工作。它不会滚动单击,或者在URL中使用锚点的页面刷新。

<nuxt-link :to="{path: '/', hash: '#projects'}">Let's go</nuxt-link>

尝试了其他一些SO答案,在NuxtConfig中添加自定义的ScrollBehaviour代码不起作用,并尝试在NUXT3中安装Vue-Scrollto,在使用Vue-Scrollto模块运行该应用程序时,我只是给了我这个错误

错误无法重新启动NUXT:未定义序列化

任何帮助将不胜感激!

完整代码

<script setup>
import '@/assets/css/main.css';

const { data } = await useAsyncData('home', () => queryContent('/').find())
const projects = data

</script>
<template>
  <div>
    <div class="flex flex-col h-screen">
    <div class="lg:p-20 p-10 text-white bg-orange-500">
      <p class="font-playfair lg:text-7xl text-4xl mb-5">Kia Ora, my name is <span class="font-medium italic">George Bates</span></p>
      <p class="font-lato lg:text-3xl text-xl mb-5">Content creator and web developer in Auckland, New Zealand</p>
    </div>
    <div class="lg:p-20 p-10 text-white flex flex-grow" style="background-image: url('images/header.jpg'); background-position: center; background-size: cover;">
    <nuxt-link :to="{path: '/', hash: '#projects'}">Let's go</nuxt-link>
    </div>
    </div>

    <main class="lg:p-20 p-10" id="projects">
      <p class="text-3xl font-playfair mb-5 font-semibold pb-2 text-orange-500">Some of my work</p>
      <Projects :projects="projects" />
    </main>
  </div>
</template>

I'm simply trying to have a page scroll to an anchor point in Nuxt3 and nothing I can do will get it to work. It doesn't scroll on click, or on page refresh with the anchor in the url.

<nuxt-link :to="{path: '/', hash: '#projects'}">Let's go</nuxt-link>

Tried a bunch of other SO answers, adding custom scrollBehaviour code to the nuxtConfig didn't work and trying to install vue-scrollTo in Nuxt3 just gave me this error when running the app with the vue-scrollTo module

ERROR Cannot restart nuxt: serialize is not defined

Any help would be greatly appreciated!

Full code

<script setup>
import '@/assets/css/main.css';

const { data } = await useAsyncData('home', () => queryContent('/').find())
const projects = data

</script>
<template>
  <div>
    <div class="flex flex-col h-screen">
    <div class="lg:p-20 p-10 text-white bg-orange-500">
      <p class="font-playfair lg:text-7xl text-4xl mb-5">Kia Ora, my name is <span class="font-medium italic">George Bates</span></p>
      <p class="font-lato lg:text-3xl text-xl mb-5">Content creator and web developer in Auckland, New Zealand</p>
    </div>
    <div class="lg:p-20 p-10 text-white flex flex-grow" style="background-image: url('images/header.jpg'); background-position: center; background-size: cover;">
    <nuxt-link :to="{path: '/', hash: '#projects'}">Let's go</nuxt-link>
    </div>
    </div>

    <main class="lg:p-20 p-10" id="projects">
      <p class="text-3xl font-playfair mb-5 font-semibold pb-2 text-orange-500">Some of my work</p>
      <Projects :projects="projects" />
    </main>
  </div>
</template>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌酱 2025-02-20 11:27:23

您说过,您已经尝试添加自定义scrollbehavior,但是您是如何做到的?

我是Vue&amp; nuxt ,但要感谢

文件app/rout.options.ts

import type { RouterConfig } from '@nuxt/schema';

// https://router.vuejs.org/api/#routeroptions
export default <RouterConfig>{
  scrollBehavior(to, from, savedPosition) {
    return {
      el: to.hash,
      behavior: 'smooth',
    };
  },
};

(在这里我提出了平稳的行为,但默认值似乎是auto

,并带有代码示例喜欢:

...
<NuxtLink :to="{path: '/', hash: '#anchor'}">Let's go!</NuxtLink>
...

You said that you already tried to add a custom scrollBehavior, but how did you do that?

I'm very new to Vue & Nuxt, but thanks to this Github answer, you can customize the scroll behavior, and this works for me:

File app/route.options.ts:

import type { RouterConfig } from '@nuxt/schema';

// https://router.vuejs.org/api/#routeroptions
export default <RouterConfig>{
  scrollBehavior(to, from, savedPosition) {
    return {
      el: to.hash,
      behavior: 'smooth',
    };
  },
};

(Here I put a smooth behavior, but default seems to be auto)

And with a sample of code like:

...
<NuxtLink :to="{path: '/', hash: '#anchor'}">Let's go!</NuxtLink>
...
屌丝范 2025-02-20 11:27:23

对我有用的是更简单。只需将以下内容添加到nuxt.config.ts文件中:

  router: {
    options: {
      scrollBehaviorType: "smooth",
    },
  },

这就是您所需要的。

您可以从该官方

希望它有帮助!

What works for me is even simpler. Just add the following into the nuxt.config.ts file:

  router: {
    options: {
      scrollBehaviorType: "smooth",
    },
  },

And that's all you need.

You can read more from this official Nuxt3 documentation under the title "Scroll Behavior for hash links".

Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文