0

What is the correct way to import jQuery and fancybox into a Vue component so it gets bundled correctly. Does it need to be at the highest App level, or just at the component using fancybox?

Most of the solutions I've seen on here use ProvidPlugin and webpack. This project does not use webpack.

I run into 'no unused' or 'not defined' problems with this:

import $ from 'jQuery'
import fancybox from 'fancybox';

My implementation is the inline vanilla varity, i.e. just adding "data-fancybox" to a link.

I have no problem if I just call jQuery and fancybox from a CDN in the index.html, but I want to learn the right way so it's bundled in.

package.json

{
  "name": "inv-dash",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "test:unit": "vue-cli-service test:unit",
    "test:e2e": "vue-cli-service test:e2e",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "airtable": "^0.7.1",
    "axios": "^0.19.0",
    "core-js": "^3.3.2",
    "fslightbox-vue": "^1.0.2",
    "imagesloaded": "^4.1.4",
    "inuitcss": "^6.0.0",
    "masonry-layout": "^4.2.2",
    "material-icons": "^0.3.1",
    "to-title-case": "^1.0.0",
    "vue": "^2.6.10",
    "vue-agile": "^1.0.11",
    "vue-carousel": "^0.18.0",
    "vue-images-loaded": "^1.1.2",
    "vue-js-toggle-button": "^1.3.3",
    "vue-lazyload": "^1.3.3",
    "vue-masonry": "^0.11.8",
    "vue-masonry-css": "^1.0.3",
    "vue-nl2br": "^0.1.2",
    "vue-router": "^3.1.3",
    "vue-select": "^3.2.0",
    "vue-toasted": "^1.1.27",
    "vue-truncate-collapsed": "^2.1.0",
    "vueisotope": "^3.1.2",
    "vuex": "^3.1.1"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^4.0.0",
    "@vue/cli-plugin-e2e-cypress": "^4.0.0",
    "@vue/cli-plugin-eslint": "^4.0.0",
    "@vue/cli-plugin-router": "^4.0.0",
    "@vue/cli-plugin-unit-jest": "^4.0.0",
    "@vue/cli-plugin-vuex": "^4.0.0",
    "@vue/cli-service": "^4.0.0",
    "@vue/eslint-config-prettier": "^5.0.0",
    "@vue/test-utils": "1.0.0-beta.29",
    "babel-eslint": "^10.0.3",
    "eslint": "^5.16.0",
    "eslint-plugin-prettier": "^3.1.1",
    "eslint-plugin-vue": "^5.0.0",
    "lint-staged": "^9.4.2",
    "node-sass": "^4.12.0",
    "prettier": "^1.18.2",
    "sass-loader": "^8.0.0",
    "vue-template-compiler": "^2.6.10"
  },
  "gitHooks": {
    "pre-commit": "lint-staged"
  },
  "lint-staged": {
    "*.{js,vue}": [
      "prettier --write",
      "git add"
    ]
  }
}
Kirk Ross
  • 6,413
  • 13
  • 61
  • 104
  • 1
    Does this answer your question? [How to use a jQuery plugin inside Vue](https://stackoverflow.com/questions/37928998/how-to-use-a-jquery-plugin-inside-vue) –  Jan 28 '20 at 19:39
  • @ChrisG The project doesn't use webpack. – Kirk Ross Jan 28 '20 at 20:42
  • Ok, so how do the files get bundled? –  Jan 28 '20 at 21:48
  • @ChrisG It's weird, there is no webpack in the package.json file but there is in the yarn.lock file... so I'm not sure. I added the package.json to my question. – Kirk Ross Jan 28 '20 at 22:53
  • If you're using Vue CLI it uses webpack under the hood. – James Totty Jan 28 '20 at 23:18
  • It does, see here: https://cli.vuejs.org/guide/webpack.html#simple-configuration –  Jan 28 '20 at 23:19
  • https://stackoverflow.com/questions/53355086/add-jquery-to-vue-cli-3 I would like to add that you should be careful when using jquery within vue components as explained in this article https://vuejsdevelopers.com/2017/05/20/vue-js-safely-jquery-plugin/ – James Totty Jan 28 '20 at 23:19

0 Answers0