简体   繁体   English

在没有 webpack 的情况下使用带有 vuejs 的 jquery 插件?

[英]Using jquery plugin with vuejs, without webpack?

I am having trouble figuring out how to use a jquery plugin within a vue component.我无法弄清楚如何在 vue 组件中使用 jquery 插件。 I am not using webkit, browserify, or ES2016.我没有使用 webkit、browserify 或 ES2016。 Is there still a way for me to be able to use a plugin and target an element within the template tag?有没有办法让我能够使用插件并定位模板标签中的元素? Google has yielded no results that were helpful.. the plugin in question is jquery.payment for stripe谷歌没有产生任何有用的结果..有问题的插件是 jquery.payment for stripe

You can use the plugin in the ready event of your component.您可以在组件的ready事件中使用该插件。 In short it would look something like this:简而言之,它看起来像这样:

new Vue({
  el: '#app',
  ready: function() {
    jQuery('.find-thing').payment('formatCardNumber')
  }
})

Here's how I'd approach it if I had no other option.如果我没有其他选择,我将如何处理它。 I did not test it but it should be relatively close:我没有测试过,但应该比较接近:

<div id="app">
  <input class="payment-input" v-model="creditCardNumber">
  <div v-if="!creditCardNumberValid">BAD CC</div>
</div>

<script>
  new Vue({
    el: '#app',
    data: {
      creditCardNumber: '',
    },
    ready: function () {
      var paymentInput = this.$el.querySelector('.payment-input');
      jQuery(paymentInput).payment('formatCardNumber')
    },
    computed: {
      creditCardNumberValid: function () {
        return jQuery.payment.validateCardNumber(this.creditCardNumber)
      }
    }
  })

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

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