I have two variables, for example, priceInUSD and priceInRub and rate USD/Ruble.
data: () => ({
priceInUSD: 0,
priceInRub: 0,
rate: 65.395402,
});
When user changing one variable, the second should be recalcualted. But If I am using watch() for both, it causing of redundant calculation calling and potentially infinity loop. One recalculating second, second calling watch for first, first calling watch for second, and at infinity.
When I am using computed and getters/setters...
computed: {
priceInUSD: {
get(){
return this.statePriceInUSD;
},
set(newValue){
this.statePriceInUSD = newValue;
this.statePriceInRub = newValue * this.rate;
}
},
priceInRub: {
get(){
return this.statePriceInRub;
},
set(newValue){
this.statePriceInRub = newValue;
this.statePriceInUSD = +newValue / this.rate;
}
},
}
... it too causing redundant recalculating of other variable.
- Changing price in USD
- Recalculating price in rubles (ok)
- Firing again recalculating price in USD (not ok)
It is possible recalculate two mutually related variables, without redunant recalculating first variable?