简体   繁体   English

将 JS 库导入 Vue.js

[英]Importing JS libraries to Vue.js

I'm trying to use hammer.js on my project, however I'm having trouble importing it.我正在尝试在我的项目中使用hammer.js ,但是我无法导入它。 I have installed the library through npm by npm i hammerjs .我已经通过npm通过npm i hammerjs安装了库。 Imported it on my main.js file as import 'hammerjs' and then when I do将它作为import 'hammerjs'到我的main.js文件中,然后当我这样做时

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

I get errors saying Hammer is not defined.我收到错误说Hammer没有定义。 What is the correct way to import libraries in vue?在 vue 中导入库的正确方法是什么?

You can include hammerjs with:您可以包含hammerjs

import * as Hammer from 'hammerjs'

A side note: You can do the same to include other libraries installed with npm in your vue project.附注:您可以执行相同的操作以在您的 vue 项目中包含安装了npm的其他库。 For example, if you want to include ThreeJS In your.js or.vue files, simply type in:例如,如果你想在你的 .js 或 .vue 文件中包含ThreeJS ,只需输入:

import * as THREE from 'three'

if you want, install the wrapper hammerjs to vue, follow the link:如果你愿意,安装包装hammerjs到vue,点击链接:

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

otherwise, do you need include the lib on the index.html, but a don't recommend.否则,您是否需要在 index.html 上包含 lib,但不推荐。

ps: I would like do comment, but i don't have reputation. ps:我想发表评论,但我没有声誉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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