question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compatibility with Vue 3

See original GitHub issue

Yesterday the final version Vue 3 was launched. But vue-currency-input is not compatible.

So I had to write some code in one personal project that redefines the plugin, component, and directive to be allowed to be used with Vue 3.

Plugin

In the old version, the $ci is defined in Vue.prototype.$ci. The new version we should define this in instance.config.globalProperties.$ci to inject the $ci variables in the instance and make this available globally. And use the redefined directive and component too.

The new plugin was defined in this way:

import {parse, getValue, setValue} from 'vue-currency-input'
import component from './component'
import directive from './directive'

export default {
  install(instance, globalOptions) {
    instance.directive('currency', directive)
    instance.component('currency-input', component)

    instance.config.globalProperties.$ci = {
      parse,
      getValue,
      setValue,
      globalOptions,
    }
  },
}

Directive

In the old version, the directive hooks are bind and componentUpdated. In the new version, the directive hooks were renamed to beforeMount and updated. So we can only rename these hooks and everything will work properly, or just wrap the old directive functions in the new directive like this:

import { CurrencyDirective } from 'vue-currency-input'

export default {
  beforeMount () {
    CurrencyDirective.bind(...arguments)
  },

  updated () {
    CurrencyDirective.componentUpdated(...arguments)
  }
}

Component

The component requires most of the changes.

In the old version, the h function is injected in the render function as the first param. In the new version, we should import the h function from the vue package, using import { h } from 'vue'. The way that the events are defined is different too. And we don’t have the directive property anymore.

return h('input', {
  onChange: () => {
    this.$emit('change', getValue(this.$el))
  },
  onInput: () => {
    const numberValue = getValue(this.$el)
      if (this.modelValue !== numberValue) {
        this.$emit('update:modelValue', numberValue)
      }
  }
})

We doesn’t have the directives property, so we have to initialize the NumberInput in the mounted hook, like:

this.$el.$ci = new NumberInput(this.$el, this.options, {
  onChange: () => {},
  onInput: () => {}
})

And as the props and events for v-model was changed. We should use the modelValue instead value, and emit 'update:modelValue' instead 'input'.

The final version of the file is this:

import {
  getValue, setValue, CurrencyInput
} from 'vue-currency-input'
import { NumberInput } from 'vue-currency-input/src/numberInput'
import { h } from 'vue'
import directive from './directive'

export default {
  extends: CurrencyInput,
  render() {
    return h('input', {
      onChange: () => {
        this.$emit('change', getValue(this.$el))
      },
      onInput: () => {
        const numberValue = getValue(this.$el)
          if (this.modelValue !== numberValue) {
            this.$emit('update:modelValue', numberValue)
          }
      }
    })
  },
  directives: {
    currency: directive
  },
  props: {
    modelValue: {
      type: Number,
      default: null
    }
  },
  mounted() {
    this.$el.$ci = new NumberInput(this.$el, this.options, {
      onChange: () => {},
      onInput: () => {}
    })

    this.setValue(this.modelValue)
  },
  methods: {
    setValue(value) {
      if (!this?.$el?.$ci) return;

      setValue(this, value)
    }
  }
}

Final Words

I think making the project compatible with Vue 3 is important, but we have these breaking changes. It would be great if we could have the Vue 3 compatible version of this library.

I hope that what I showed here helps us to have the compatible version, or helps anyone who wants to use this library with Vue 3.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:37 (16 by maintainers)

github_iconTop GitHub Comments

15reactions
dm4t2commented, Sep 19, 2020

Hi @maxmaccari, thank you for summing up 👍

I have also followed the progress of Vue 3 with great interest and just want to let everyone know that I’m currently working on a compatible version. A beta version will be released shortly.

11reactions
dm4t2commented, Oct 10, 2020

I just published a pre-release 2.0.0-beta.0 compatible with Vue 3 🎉

Any feedback is appreciated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migration Build
@vue/compat (aka "the migration build") is a build of Vue 3 that provides configurable Vue 2 compatible behavior. The migration build runs in...
Read more >
What New Features are in Vue 3 | GrapeCity JavaScript
Vue 3 was officially released on September 18, 2020. While it has some great new features, it's also completely backward-compatible with Vue ......
Read more >
Which UI Frameworks Support Vue 3? - In Plain English
AgnosticUI (26 stars on GitHub) ; Amplify UI (139 stars on GitHub) ; Ant Design for Vue (15.7k stars on GitHub) ; Element...
Read more >
Backwards compatibility and Vue 3 : r/vuejs - Reddit
Vue 3 is compatible with the options api. Vue 2 has a plug-in for something like composition api (and gets it without plug-in...
Read more >
Vue 3 – A roundup of infos about the new version of Vue.js
However, it won't break anything in your Vue 2.x apps, as Vue 3 is still 100% compatible with the current syntax / the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found