A
A
Ankozar2021-08-23 13:37:32
Vue.js
Ankozar, 2021-08-23 13:37:32

How to import jQuery for all Vue components?

I am translating a project from js and jQUery to view.

I want jQUery to work for now. I will replace the functionality gradually.
I read that you can just import it into a component. And you can install via npm.
Actually, I installed it. At import in a component works.

But when importing in main.js and using it in new Veu, I don’t understand how to access it from the components.

//main.js:

import $ from 'jquery'

////

new Vue({
  $,
  router,
  store,
  axios,
  VueAxios,
  render: function (h) { return h(App) }
}).$mount('#app')


How to use it later in components?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex_mos, 2021-08-23
@Alex_mos

I think you will find a solution here https://coderoad.ru/53355086/%D0%94%D0%BE%D0%B1%D0... , as I understand it, you need to install ProvidePlugin and write in vue.config

const webpack = require('webpack')
module.exports = {
  configureWebpack: {
    plugins: [
      new webpack.ProvidePlugin({
        $: 'jquery',
        jquery: 'jquery',
        'window.jQuery': 'jquery',
        jQuery: 'jquery'
      })
    ]
  },
}
and then in main.js writewindow.$ = window.jQuery = require('jquery');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question