Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular NPM package not functioning within Vue.js app

Tags:

vue.js

nuxt.js

I'm trying to use NPM package scroll-ease-efficient in my Nuxt/Vue app, so I've created a file called scroll-ease-efficient.client.js in plugins/ folder that I specified in the plugins section of my nuxt.config.js file.

Here is the content of the file:

import Vue from 'vue'
import { scrollTo } from 'scroll-ease-efficient'
Vue.use(scrollTo)

Then in my app I simply do:

const scrollEle = document.getElementById('element')
scrollTo(scrollEle, 500)

This should work but it does nothing, and I have no console error either.

What's wrong?

like image 372
drake035 Avatar asked Dec 01 '25 23:12

drake035


1 Answers

thanks for asking. But first, let me clarify. Not all npm package is intended to be installed using Vue/Nuxt plugin method. The regular npm package name for this way often contains word vue/nuxt, like Vuetify or Vuefire.

And scroll-ease-efficient package isn't vue nor nuxt plugin. So in order to use this package, you can add global api from that package to vue or nuxt constructor.

import Vue from "vue";
import { scrollTo } from "scroll-ease-efficient";

Vue.prototype.$scrollTo = scrollTo;

Now you can access the global method through this.$scrollTo()


Edit

Please checkout this link for the demo: https://codesandbox.io/s/vue-scroll-ease-efficient-demo-8tqmj

like image 79
Jefry Dewangga Avatar answered Dec 05 '25 00:12

Jefry Dewangga