Created
October 25, 2022 14:49
-
-
Save AhmedElwerdany/ee84922a496b4052d6b6d109b38de27c to your computer and use it in GitHub Desktop.
watch array in vue.js v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://ahmedelwerdany.github.io/posts/watch-many-in-vue-js/ | |
function watchMany (arrayOfKeys, value) { | |
const object = {} | |
for (let i = 0; i < arrayOfKeys.length; i++) { | |
object[arrayOfKeys[i]] = value | |
} | |
return object | |
} | |
export default { | |
name: 'Form', | |
data: () => ({ | |
shose_price: 0, | |
hat_price: 0, | |
short_price: 0, | |
}), | |
watch: { | |
...watchMany(['shose_price', 'hat_price','short_price'], function (value) { | |
if (typeof value !== 'number') { | |
return | |
} | |
this.total = this.shoes_price + this.short_price + this.hat_price | |
}), | |
total : function () { | |
this.total_with_disscount = this.total - (this.total * this.discount) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment