Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript nuxt链接导航到带有哈希/锚的页面时出现问题_Javascript_Vue.js_Vue Router_Nuxt.js - Fatal编程技术网

Javascript nuxt链接导航到带有哈希/锚的页面时出现问题

Javascript nuxt链接导航到带有哈希/锚的页面时出现问题,javascript,vue.js,vue-router,nuxt.js,Javascript,Vue.js,Vue Router,Nuxt.js,我在网站导航中使用nuxt链接,大多数链接指向主页中的散列元素/锚,如: <nuxt-link v-else class="text-link" :to="localePath('index') + #hash" > 在:to属性中,我尝试了“hash”#hash,但没有任何效果。谁能帮我一下吗?如何导航到另一个页面+#锚定(以便滚动到该锚定?如果您使用numxt Link,出现此问题的任何人都是一个简单的解决方案 而不是使用 联系不起作用的。 使用联系人 path是您

我在网站导航中使用nuxt链接,大多数链接指向主页中的散列元素/锚,如:

<nuxt-link
  v-else
  class="text-link"
  :to="localePath('index') + #hash"
>

在:to属性中,我尝试了“hash”#hash,但没有任何效果。谁能帮我一下吗?如何导航到另一个页面+#锚定(以便滚动到该锚定?

如果您使用
numxt Link,出现此问题的任何人都是一个简单的解决方案

而不是使用
联系不起作用的
。 使用
联系人

path
是您将导航到的页面

hash
是您将滚动到的位置


希望这对某人有所帮助。

使用这种方式,使用getBoundingClientRect().top+window.scrollY,可以更好地解决以下问题:

if(to.hash){
let el=wait findEl(to.hash)
if(document.documentElement.style中的('scrollBehavior')){
返回window.scrollTo({top:el.getBoundingClientRect().top+window.scrollY,行为:'smooth'})
}否则{
返回window.scrollTo(0,el.getBoundingClientRect().top+window.scrollY)
}
}

有点晚了,但这正是我能够做到的。感谢Sivuyile TG Magutywa

 <nuxt-link :to="{ path: localePath('index'),hash:'#about'}">
          {{ $t("menu.about") }}
 </nuxt-link>

如果您使用的是nuxt版本2.4+,那么您应该如何设置滚动行为,特别是底部的“返回承诺”部分。@Andrew1325,当我单击mu nuxt链接时,滚动到该元素的行为永远不会发生。单击不会触发任何内容。只是在Nuxt的版本
^2.0.0
中测试过,似乎不起作用。在同一版本@FooBar上对我有效,尽管它没有滚动到div,但它只是直接转到哈希。我没有弄乱默认的路由器滚动行为。救命恩人!一个传奇!
 <nuxt-link :to="{ path: localePath('index'),hash:'#about'}">
          {{ $t("menu.about") }}
 </nuxt-link>
https://www.shrek.com/fr/about