vue.jsvuejs2vuetify.js

How to use Vue-IMask with Vuetify in Vue.js 2.5+


How can I use Vue-IMask directive on Vuetify controls specially v-text-field? It works on HTML input but I got error on v-text-field.

enter image description here

enter image description here


Solution

  • The vue-imask directive appears to be incompatible with Vuetify elements because it expects the element to be an <input>, but <v-text-field> resolves to a <div> (with several inner elements that comprise a custom input). While the docs show an example where you could use IMask via a computed property, potentially bound to any v-model (perhaps including <v-text-field>), I was not able to get the example to work with any element.

    You might consider using <v-text-field> with a different masking library, such as vue-the-mask, as recommended in the Vuetify docs.

    <!DOCTYPE html>
    <html>
    <head>
      <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
      <link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet">
      <link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
      <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
    </head>
    <body>
      <div id="app">
        <v-app>
          <v-content>
            <v-text-field v-mask="['(###)###-####', '#-###-###-####', '###-###-####']" placeholder="Enter phone number" />
          </v-content>
        </v-app>
      </div>
    
      <script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>
      <script src="https://unpkg.com/vue-the-mask@0.11.1/dist/vue-the-mask.js"></script>
      <script>
        Vue.use(VueTheMask)
        
        new Vue({
          el: '#app',
          vuetify: new Vuetify()
        })
      </script>
    </body>
    </html>