Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MartinX3 opened this issue Aug 3, 2022 · 4 comments
Open

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

MartinX3 opened this issue Aug 3, 2022 · 4 comments
Labels

Comments

@MartinX3
Copy link

MartinX3 commented Aug 3, 2022

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
Copy link
Member

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

@danielroe danielroe added enhancement New feature or request workaround available and removed pending triage labels Aug 3, 2022
@MartinX3
Copy link
Author

MartinX3 commented Aug 3, 2022

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
Copy link

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
Copy link
Author

MartinX3 commented Aug 4, 2022

There is no overflow in my code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants