[英]Vue JS routing issue - scroll behavior not working correctly
I have a Vue based site where I use Vue Router, but I have some problems with it. 我有一个使用Vue路由器的基于Vue的站点,但是我有一些问题。 What I try to create is when the user clicks the a button it navigates back to the first page to a specific anchor.
我试图创建的是当用户单击a按钮时,它导航回到第一页并到达特定锚点。
I tried to use the Vue build in Scroll Behavior, but it is not working, It only navigates back to the main page, but not to the specific anchor. 我尝试在Scroll Behavior中使用Vue构建,但是它不起作用,它仅导航回到主页,而不导航到特定锚点。
I also tried this way to solve the issue: https://dev.to/napoleon039/the-lesser-known-amazing-things-vuerouter-can-do-25di 我也尝试过这种方法来解决问题: https : //dev.to/napoleon039/the-lesser-known-amazing-things-vuerouter-can-do-25di
Here is my router file: 这是我的路由器文件:
import Vue from 'vue'
import VueRouter from 'vue-router'
import home from '@/components/home'
import works from '@/components/works'
Vue.use(VueRouter)
export default new VueRouter({
mode: 'history',
routes: [
{
path: '/',
name: 'home',
component: home
},
{
path: '/works',
name: 'works',
component: works
},
],
scrollBehavior(to, from, savedPosition) {
if (savedPosition) {
return savedPosition
}
if (to.hash) {
return { selector: to.hash }
}
return { x: 0, y: 0 }
}
})
This the how I use the router link: 这是我如何使用路由器链接:
<router-link :to="'/#main-page'">
I also tried this way: 我也尝试过这种方式:
<router-link :to="{ path: '/', hash:'#main-page' }">
And this is where I want to navigate: 这是我要导航的地方:
<section class="intro" id="main-page">
<div class="intro--left">
<img />
</div>
</section>
只需将参数添加到nuxt-link的:to =或this。$ router.push()中,如下所示:
{ path: '/something', params: { foo: 'bar'}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.