我发现这篇文章:https ://vuejsdevelopers.com/2017/05/20/vue-js-safely-jquery-plugin/
其中解释了如何将 JQuery 与 Vue 一起使用。我遵循该模式并将 Vue 与 Cleave 集成如下:
SmartCleave.vue
<template>
<input />
</template>
<script>
/* eslint-disable no-new */
import Cleave from 'cleave.js'
export default {
mounted () {
new Cleave(this.$el, {
date: true,
datePattern: ['d', 'm', 'Y']
})
this.$el.oninput = (e) => {
console.log('oninput the field', e.target.value)
this.$emit('oninput', e.target.value)
}
}
}
</script>
应用程序.vue
<template>
<div id="app">
<smart-cleave @oninput="logIt"></smart-cleave>
<div>{{date}}</div>
</div>
</template>
<script>
/* eslint-disable no-new */
import Cleave from 'cleave.js'
import SmartCleave from './components/SmartCleave'
new Cleave('#plain-input', {
date: true,
datePattern: ['d', 'm', 'Y']
})
export default {
name: 'app',
components: {
SmartCleave
},
data () {
return {
date: ''
}
},
methods: {
logIt (val) {
console.log('cahnged', val)
this.date = val
}
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
/* text-align: center; */
color: #2c3e50;
margin-top: 60px;
}
</style>