Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Vue 3 support request (Plugin load fail in Vue3) #498

Open
tampler opened this issue Sep 26, 2020 · 16 comments
Open

Vue 3 support request (Plugin load fail in Vue3) #498

tampler opened this issue Sep 26, 2020 · 16 comments

Comments

@tampler
Copy link

tampler commented Sep 26, 2020

Hello
I'm new to Vue and trying to use v-mask in my project. I was following the blog post, but the plugin fails on load.

System
OS: Ubuntu 18.04
npm: 6.14.8
node: 14.12.0
browser: Chromium 85.0.4183.83

Loading with main.js:

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

createApp(App).use(store).use(router).mount('#app')

Error code:

v-mask.esm.js?b6d0:501 Uncaught TypeError: Vue.filter is not a function
    at plugin (v-mask.esm.js?b6d0:501)
    at Object.use (runtime-core.esm-bundler.js?5c40:3364)
    at eval (main.js?56d7:6)
    at Module../src/main.js (app.js:1129)
    at __webpack_require__ (app.js:849)
    at fn (app.js:151)
    at Object.1 (app.js:1142)
    at __webpack_require__ (app.js:849)
    at checkDeferredModules (app.js:46)
    at app.js:925

Thanks for supporting this great plugin !

Boris

@probil probil self-assigned this Sep 27, 2020
@probil
Copy link
Owner

probil commented Sep 27, 2020

Hey @tampler . v-mask doesn't support Vue 3 yet. Since Vue 3 has been released recently I suppose it's time to make library compatible with it. Thanks for the issue

@nizamutdinov
Copy link

@probil Hey

VUE 3.0 requires two major changes:

1. Directives are now declared differently.

2. "Filters" have been removed. Instead, it is suggested to use "computed" properties.

After making these changes, the directives works as before.

But still, the filter needs to be replaced with a computed property.

Hope it helps.

@ux-engineer
Copy link

Vue Core 3.0 has been released a couple of months ago, with full 'latest' tagged release with all the official packages maturing as stable, aimed at the end of the year.

@probil as there is less than a month of time left, have you been planning for this upgrade?

@dinoluck
Copy link

Are we abandoning this package for Vue 3?

@dgoldstein89
Copy link

dgoldstein89 commented Apr 26, 2021

To add to what @nizamutdinov was saying, there's also the issue of hook functions for directives being renamed.
The following should work to use as a directive in Vue 3:

import { VueMaskDirective } from 'v-mask';

const vMaskV2 = VueMaskDirective;
const vMaskV3 = {
    beforeMount: vMaskV2.bind,
    updated: vMaskV2.componentUpdated,
    unmounted: vMaskV2.unbind
};

createApp(...)
   .directive('mask', vMaskV3)
   .mount(...); 

@probil probil changed the title Plugin load fail in Vue3 Vue 3 support request (Plugin load fail in Vue3) Apr 27, 2021
@probil probil pinned this issue Apr 27, 2021
@leemcd56
Copy link

To add to what @nizamutdinov was saying, there's also the issue of hook functions for directives being renamed.
The following should work to use as a directive in Vue 3:

import { VueMaskDirective } from 'v-mask';

const vMaskV2 = VueMaskDirective;
const vMaskV3 = {
    beforeMount: vMaskV2.bind,
    updated: vMaskV2.componentUpdated,
    unmounted: vMaskV2.unbind
};

createApp(...)
   .directive('mask', vMaskV3)
   .mount(...); 

Thank you so much! This really helped!

@diego-lipinski-de-castro

what about filters tho? Anyway to use it? @dgoldstein89

@dgoldstein89
Copy link

Like @nizamutdinov said:

"Filters" have been removed. Instead, it is suggested to use "computed" properties.

I haven’t looked into it further.

@ux-engineer
Copy link

@dgoldstein89 https://v3.vuejs.org/guide/migration/filters.html#global-filters

@Chuuone
Copy link

Chuuone commented Nov 17, 2021

While this temporary solution works, how would you go about adding custom placeholders?

In vue 2
Vue.use(VueMask, { placeholders: { H: /[0-2]{1}/, h: /[0-9]{1}/, M: /[0-5]{1}/, m: /[0-9]{1}/, S: /[0-5]{1}/, s: /[0-9]{1}/ } });

Edit 1
I resolved my requirement for now by copying the library and exposing the createDirective function. You can also go as far as remove the unused exports to make the custom package smaller.
In v-mask/src/index.js

import {directive, createDirective} from './directive';
import filter from './filter';
import plugin from './plugin';

export {
  plugin as default,
  plugin as VueMaskPlugin,
  filter as VueMaskFilter,
  directive as VueMaskDirective,
  createDirective
};

The plugin seems to work as before. Just have to be sure to not use the new v-model syntax i.e. "v-model:custom-value"
my-custom-installer.js

import {createDirective} from "../libraries/v-mask-custom";

export default function install(application) {
	const directive = createDirective({
		placeholders: {
			H: /[0-2]{1}/,
			h: /[0-9]{1}/,
			M: /[0-5]{1}/,
			m: /[0-9]{1}/,
			S: /[0-5]{1}/,
			s: /[0-9]{1}/
		}
	});

	application.directive("mask", {
		beforeMount: directive.bind,
		updated: directive.componentUpdated,
		unmounted: directive.unbind
	});
}

Attaching to application.

createApp({}).use(VueMaskCustom)

@amchconsult
Copy link

any news about v-mask for Vue 3?
thank you

@probil
Copy link
Owner

probil commented Jan 30, 2022

Vue 3 release migration checklist:

  • Update dependencies to use Vue 3
  • Update hook method names
  • Expose filter as a function to use as computed + tests
  • Update unit-tests (includes @vue/test-utils upgrade to v2)
  • Update e2e tests
  • Update docs

@jboada
Copy link

jboada commented Aug 7, 2022

Hello,

Any news on the migration?

Best Regards.
JB

@srsimonson
Copy link

https://www.npmjs.com/package/maska

I was able to replace v-mask with maska. I didn't even have to change my masking conventions, it was literally a 1:1 swap, app.use(Maska) then added an "a" at the and of all references to v-mask to make v-maska and that was it.

I doubt everyone will have such a seamless transition, but Maska is worth a shot!

@trivonse
Copy link

trivonse commented Jan 18, 2023

https://www.npmjs.com/package/maska

I was able to replace v-mask with maska. I didn't even have to change my masking conventions, it was literally a 1:1 swap, app.use(Maska) then added an "a" at the and of all references to v-mask to make v-maska and that was it.

I doubt everyone will have such a seamless transition, but Maska is worth a shot!

@srsimonson, can you show how to use it, bcs it didn't work for me, thanks

@anujagrawal4
Copy link

Hello,

Is there any update on Vue3 compatibility? Thank you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests