简体   繁体   中英

Importing JS libraries to Vue.js

I'm trying to use hammer.js on my project, however I'm having trouble importing it. I have installed the library through npm by npm i hammerjs . Imported it on my main.js file as import 'hammerjs' and then when I do

var hammertime = new Hammer(myElement, myOptions)
hammertime.on('pan', function(ev) {
console.log(ev)
})

I get errors saying Hammer is not defined. What is the correct way to import libraries in vue?

You can include hammerjs with:

import * as Hammer from 'hammerjs'

A side note: You can do the same to include other libraries installed with npm in your vue project. For example, if you want to include ThreeJS In your.js or.vue files, simply type in:

import * as THREE from 'three'

if you want, install the wrapper hammerjs to vue, follow the link:

https://www.npmjs.com/package/vue2-hammer

otherwise, do you need include the lib on the index.html, but a don't recommend.

ps: I would like do comment, but i don't have reputation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM