How to use jquery plugin in vuejs2

1

I'm having difficulty making settings for the component using jquery in vuejs2.

<div id="app">
        <date-picker @update-date="updateDate" timepicker= "false" v-once></date-picker>
        <p>{{ date }}</p>
 </div>

<script>

Vue.component('date-picker', {
    template: '<input/>',
    props: [ 'timepicker' ],
    mounted: function() {
    var self = this;
    $(this.$el).datetimepicker({
      timepicker: this.timepicker,

      onSelect: function(date) {
        self.$emit('update-date', date);
      }
    });
    },
    beforeDestroy: function() {
      $(this.$el).datetimepicker('hide').datetimepicker('destroy');
    }
  });

  new Vue({
    el: '#app',
    data: {
      date: null
    },
    methods: {
      updateDate: function(date) {
        this.date = date;
      }
    }
  });
    
asked by anonymous 16.10.2017 / 15:23

1 answer

0

Option # 1: Using ProvidePlugin

Add ProvidePlugin in the array of plugins in files build/webpack.dev.conf.js and build/webpack.prod.conf.js , so jQuery becomes global for all your modules:

plugins: [

  // ...

  new webpack.ProvidePlugin({
    $: 'jquery',
    jquery: 'jquery',
    'window.jQuery': 'jquery',
    jQuery: 'jquery'
  })
]

Option # 2: Use Expose Loader module for webpack

Add the Expose Loader package:

npm install expose-loader --save-dev

Use no entry point of your main.js as follows:

import 'expose?$!expose?jQuery!jquery'

// ...
    
16.10.2017 / 23:40