A
A
alex5e2017-01-11 09:49:34
JavaScript
alex5e, 2017-01-11 09:49:34

How to include third-party libraries (like jquery, form-serializer, etc.) in vue.js?

Unable to include third party libraries in vue.js
main.js

...
    require('jquery')
    import Vue from 'vue'
    import VueMdl from 'vue-mdl'
    import App from './App'
    import Auth from './views/user/Auth'
    import Index from './views/Index'
    import VueRouter from 'vue-router'
    import VueResource from 'vue-resource'

    Vue.use(VueRouter)
    Vue.use(VueMdl)
    Vue.use(VueResource)

    const AppComponent = Vue.extend(App)
    new AppComponent({
      router,
      el: '#app',
      template: '<App/>',
      components: { App }
    })

RegisterModal.vue
<template>
  ...
</template>

    <script>
      export default {
        name: 'register',
        data () {
          return {
            msg: 'Register'
          }
        },
        methods: {
          open () {
            this.$refs.register.open()
          },
          close () {
            this.$refs.register.close()
          },
          submit () {
            var data = $('form').serialize() //  '$' is not defined
            console.log(jQuery) // 'jQuery' is not defined
            console.log(data);
          }
        }
      }
    </script>

Connecting in webpack config doesn't work either
plugins: [
new webpack.ProvidePlugin({
$ : "jquery",
jQuery : "jquery",
})
],

Tell me, maybe I missed something?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander Aksentiev, 2017-01-11
@Sanasol

missed the fact that there is jquery + bootstrap + lodash connection out of the box

E
Eugene, 2017-01-11
@AppFA

So you explicitly write imports in RegisterModal.vue itself, then what you wrote jquery import in main.js, so it will only be visible in the scope of this file:

<script>
import $ from 'jquery';

export default {
    name: 'register',
    ...
}
</script>

S
shelomanovd, 2017-05-21
@shelomanovd

in index.html connect locally. Or in index.js import

G
Gennadiy Balachkov, 2017-07-14
@gennadiy403

It is enough to write in index.html
script src=" https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.... "

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question