S
S
SevSergei2018-02-10 15:44:09
JavaScript
SevSergei, 2018-02-10 15:44:09

How to keep track of two fields in Vue.js so they don't fire in sequence?

I am writing a calculator
. There are 2 fields with v-model that I follow through watch.
Each watch has its own function that is processed when one field changes and is written to another.
The problem is that the function from watch is launched immediately (field A changes B, B changes A), a kind of mini recursion.
How can I make it so that when the data is changed by the user, watch is triggered, but not when I write the data?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
SevSergei, 2018-02-10
@SevSergei

if it helps anyone:
watch - use when there is only 1 v-model parameter and other inputs do not depend on it
computed (get, set) + storage variable - when the data depends on each other
I will explain in more detail:
I had 2 inputs, you need to when the field data (any) was changed, the
USD rate was recalculated, the RUB rate
used watch and it turned out that due to an error, the script fell into a cycle, because followed two fields
if to use computed - that everything is OK.
thanks to all

D
Dima Polos, 2018-02-10
@dimovich85

I also didn’t quite understand the problem, but if I understood correctly, then add a modifier to the directive: v-model.lazy="..." This modifier subscribes to the change event, not to the input event, and the change event occurs after value has changed and input is terminated, eg input passed and focus lost. Read more here.

0
0xD34F, 2018-02-10
@0xD34F

If I understood correctly, then you don't need watch here - watch what the user enters by handling the input event.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question