NUXT3 -NUXTLINK锚定不滚动单击或页面刷新
我只是尝试将页面滚动到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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您说过,您已经尝试添加自定义
scrollbehavior
,但是您是如何做到的?我是Vue&amp; nuxt ,但要感谢
文件
app/rout.options.ts
:(在这里我提出了平稳的行为,但默认值似乎是
auto
),并带有代码示例喜欢:
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
:(Here I put a smooth behavior, but default seems to be
auto
)And with a sample of code like:
对我有用的是更简单。只需将以下内容添加到nuxt.config.ts文件中:
这就是您所需要的。
您可以从该官方
希望它有帮助!
What works for me is even simpler. Just add the following into the nuxt.config.ts file:
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!