In Vue.js I have three templates that should work together to trigger loading of new results. My container template is a general template that contain the filters and the results templates.
I am having some difficulties with making watch() trigger changes in my results template when an object is changed in the filters template. The flow is quite simple. Here is what I have at the moment and how the flow works:
Filters template: on click my object property is updated with a new value and emit this change to the container template.
<span v-on:click='setClient(client.ccid)'>{{client.name}}</span>
data: function() {
return {
formData: {
perPage: 15,
clientId: null
}
}
}
setClient: function(clientId){
this.formData.clientId = clientId;
this.$emit('filters-update', this.formData);
}
Container template: - this has just the role to hold the filters and results template and pass the modified object to the results template.
<template>
<div class="row">
<filters v-on:filters-update="filtersUpdate"></filters>
<results :filters='filters'></results>
</div>
</template>
<script>
export default {
data: function() {
return {
filters: {}
}
},
methods: {
filtersUpdate: function(params){
this.filters = params;
}
}
}
</script>
Results template:
export default {
props: {
filters: {
type: Object
},
}
}
watch: {
filters: function(){
console.log("loading new results..");
this.loadData();
}
}
filtersUpdateis being called? 2. Is thewatchnever called, or is it only called once? It seems you'd be emitting the same object every time, so I'd only expect it to trigger thewatchthe first time. 3. The code you posted for theresultscomponent has thewatchsection outside the component options. I assume that is inside the options object in your real code?filtersUpdateis called. 2) The watch is called initially when the filters template is created, I am emitting theformDataobject with the number of results per page to get in theresultstemplate. This works as expected, and the watch is triggered. 3) That was my bad with posting brackets, the watch is actually inside theexport defaultobjectdeep: true, handler(){ this.loadData(); }