N
N
Nikita Pavlyuk2021-05-23 20:49:09
Vue.js
Nikita Pavlyuk, 2021-05-23 20:49:09

Best way to integrate BigNumber.js into Vue project?

How can BigNumber.js be integrated into the project for use in forms?

Option 1 : You can use computed getters and setters, where in setters wrap in value in BigNumber -

// emplate
<input v-model="fieldValue" />
// computed
fieldValue: {
   get() { return this.form.count.toString() },
   set(v) { this.form.count = new BigNumber(v) }
}

The disadvantage of the first option is that for each field you need to make a computed property with getters and setters.

Option 2 A wrapper component that would encapsulate the conversion of a regular numeric type to bigNubmer and vice versa. Minus: The need to duplicate all the internal logic of the component, but for bigNumber numbers. + Specifically, in my case I'm using a third party component

Option 3 Use @change event and :value attribute on input component instead of v-model. Those. in the change handler, wrap the entered number in BigNumber and get its value in value. minus: we do not use v-model, which, as for me, is much more concise

. What other solutions can you suggest? Thanks in advance!

lib link https://github.com/MikeMcl/bignumber.js

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2021-05-23
@Neakit

1+2. A wrapper component that uses a method with a getter-setter. No logic needs to be rewritten.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question