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

using this.$scrollTo in nuxt / typescript #321

Open
avxkim opened this issue Jul 11, 2020 · 2 comments
Open

using this.$scrollTo in nuxt / typescript #321

avxkim opened this issue Jul 11, 2020 · 2 comments
Labels
help wanted nuxt Issues related to nuxt.

Comments

@avxkim
Copy link

avxkim commented Jul 11, 2020

There's an issue when using with nuxt (2.13.3) / typescript (@nuxt/types => 2.13.3)

If i don't import vue-scrollto inside my global.d.ts file:

import 'vue-scrollto'

Then using this.$scrollTo inside a component would complain about:

"Property '$scrollTo' does not exist on type 'CombinedVueInstance<Vue, Data, { setActive(index: number, id: number): void; findArticles: ((this: any, val: string) => Promise) & Cancelable; hideResults(): void; showArticle(articleId: number, categoryId: number): Promise<...>; }, unknown, Readonly<...>>'.Vetur(2339)"

@rigor789 rigor789 added help wanted nuxt Issues related to nuxt. labels Jul 11, 2020
@picbenoit
Copy link

@webcoderkz

For sure too late, but you need to add "vue-scrollto" to the types in your tsconfig.json file.

"types": [
  "@types/node",
  "@nuxt/types",
  "@nuxtjs/axios",
  "vue-scrollto",
]

If it can help someone.

@vinayakkulkarni
Copy link

FYI, for folks who wanna use it with Nuxt + TS + Vue Composition API
/plugins/scroll-to.ts:

import { getCurrentInstance } from '@vue/composition-api';

export function useScrollTo() {
  const { $scrollTo } = getCurrentInstance();

  if (!$scrollTo) {
    // throw error, no store provided
    throw new Error('Nuxt $scrollTo is not defined!');
  }
  return $scrollTo;
}

add it to plugins: [] in nuxt.config.(j|t)s:

  plugins: [
    // ...
    { src: '~/plugins/scroll-to', mode: 'client' },
    // ...
  ],
  modules: [
    // ...
	// Docs: https://vue-scrollto.netlify.app/docs/
    'vue-scrollto/nuxt',
    // ...
  ],

and using it in your component:

<template>
  <table>
    <tbody>
      <tr
        v-for="({ id, address }, index) in state.someArray"
        :id="`scroll-${index}`"
        :key="index"
      >
        <td>
          {{ id }}
        </td>
        <td>
          {{ address }}
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script lang="ts">
  import { defineComponent, onMounted, reactive } from '@vue/composition-api';
  import { useScrollTo } from '@/plugins/scroll-to';

  export default defineComponent({
    name: 'VueScrollToDemo',
    setup() {
      const $scrollTo = useScrollTo();
      const state = reactive({
        someArray: [
          {
            id: 1,
            address: 'ABC',
          },
          {
            id: 2,
            address: 'XYZ',
          },
          {
            id: 3,
            address: '123',
          },
        ]
      });

      onMounted(() => {
        scrollToRandom(2);
      });

      function scrollToRandom(idx: number) {
        $scrollTo(`#scroll-${idx}`, 500, {
          container: '.__panel',
          offset: -100,
        });
      }
      return { state };
</script>

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

No branches or pull requests

4 participants