My API provides me a hash which I receive as part of an AJAX call. The content of the AJAX response (which includes the hash) is updating data
components in my Vue
instance (so that the DOM is modified, per usual Vue usage).
I was wondering if it is possible to trigger (run) a function upon the change of a specific data
element. Reactivity in Depth does not mention this and for me (please correct me if this is wrong) computed
and methods
are a way to indirectly provide new computed elements for the DOM (in other words, they do not start because a specific element was modified but are rather synchronisation methods between data
and other variables provided to the DOM).
I was hoping for something along the lines of (this is non-working, incorrect pseudo-code, I just add it to put in context of a Vue instance):
var vm = new Vue({
el: '#root',
data: {
hash: null
},
functions_to_trigger_upon_an_element_change: {
hash: function () {
location.reload()
}
}
})
The idea above would be to have location.reload()
run when the value of hash
changes.
Is there such a mechanism in Vue?
If there is not, I will keep the state independently of Vue and act accordingly upon its change but it would be nice to reuse the Vue watch properties to do that.
A Watcher in Vue. js is a special feature that allows one to watch a component and perform specified actions when the value of the component changes. It is a more generic way to observe and react to data changes in the Vue instance. Watchers are the most useful when used to perform asynchronous operations.
Reactive dependencies are those that are observable, like data properties or Vuex state. Non-reactive dependencies would be something like a simple variable or something from local-storage.
Vue comes with the reactive method, which is used to create a reactive state in JavaScript or Vue. Basically, the reactive method is just a function that creates a proxy and wraps it around provided data objects, ultimately turning it into a proxied object.
It must be a function because otherwhise the data will be shared among all instances of the component, as objects are call by reference rather than call by value. This does not only happen when you reference a global object but also when data is an object itself.
You can use a watch, like the following example:
var vm = new Vue({
el: '#demo',
data: {
firstName: 'Foo',
lastName: 'Bar',
fullName: 'Foo Bar'
},
watch: {
firstName: function (val) {
this.fullName = val + ' ' + this.lastName
},
lastName: function (val) {
this.fullName = this.firstName + ' ' + val
}
}
})
About your case, here is the example about how it could be implemented.
var vm = new Vue({
el: '#demo',
data: {
hash: 'Foo'
},
watch: {
hash: function (val) {
// when the hash prop changes, this function will be fired.
location.reload()
}
}
})
I hope I understood your question right.
I think this should work
var vm = new Vue({
el: '#root',
data: {
hash: null
},
watch: {
hash: function (val) {
functions_to_trigger_upon_an_element_change()
}
},
methods: {
functions_to_trigger_upon_an_element_change() {
// You code
}
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With