nuxt / bridge

🌉 Experience Nuxt 3 features on existing Nuxt 2 projects
MIT License
273 stars 29 forks source link

[composition api][typescript] `scrollToTop` per route not available #457

Open MartinX3 opened 2 years ago

MartinX3 commented 2 years ago

Environment

Reproduction

N/A

I just saw https://nuxtjs.org/docs/components-glossary/scrolltotop/ but with the composition api it's impossible to use

<script>
  export default {
    scrollToTop: true
  }
</script>

Nuxt default scroll behaviour doesn't work if I can't set this option in the composition api: https://nuxtjs.org/docs/configuration-glossary/configuration-router/#scrollbehavior

Describe the bug

The scroll position is always the same. I want to scroll to top on new routes and if going back I want to restore my previous scroll position.

Additional context

I use this workaround for now:

component back button

import { getCurrentInstance } from 'vue'
getCurrentInstance()?.proxy?.$router.go(-1)

src/app/router.scrollBehavior.js

export default function (_, __, savedPosition) {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve({
                x: savedPosition?.x || 0,
                y: savedPosition?.y || 0,
            })
        }, 0)
    })
}

Logs

N/A
danielroe commented 2 years ago

The best way is to use two <script> blocks for these kind of features based on component options in Nuxt 2.

MartinX3 commented 2 years ago

In the specific child route I tried

<script lang="ts">
export default {
    scrollToTop: true,
}
</script>

but it always pushes me to the top using the back button. If I remove it, it always uses the same scroll position.

So it doesn't work without the workaround.

antlionguard commented 2 years ago

In the specific child route I tried

<script lang="ts">
export default {
    scrollToTop: true,
}
</script>

but it always pushes me to the top using the back button. If I remove it, it always uses the same scroll position.

So it doesn't work without the workaround.

Nuxt always scroll top when route is changed. You have any overflow: hidden, overflow-y: hidden or overflow-x: hidden css line for body/html? If is exist you should try to remove them.

MartinX3 commented 2 years ago

There is no overflow in my code.