Within my main application, I've implemented a Modal
component that receives content via an event whenever a modal needs to be displayed. The Modal content consists of a list with an associated action for each item, such as "select" or "remove":
Vue.component('modal', {
data() {
return {
shown: false,
items: [],
callback: ()=>{}
}
},
mounted() {
EventBus.$on('showModal', this.show);
},
template: `<ul v-if="shown">
<li v-for="item in items">
{{ item }} <button @click="callback(item)">Remove</button>
</li>
</ul>`,
methods: {
show(items, callback) {
this.shown = true;
this.items = items;
this.callback = callback;
}
}
});
However, when passing a computed property to the modal like in the following component, the reactive connection is lost -> causing the list not to update if the action is "remove".
Vue.component('comp', {
data() {
return {obj: {a: 'foo', b: 'bar'}}
},
computed: {
objKeys() {
return Object.keys(this.obj);
}
},
template: `<div>
<button @click="showModal">Show Modal</button>
<modal></modal>
</div>`,
methods: {
remove(name) {
this.$delete(this.obj, name);
},
showModal() {
EventBus.$emit('showModal', this.objKeys, this.remove);
}
}
});
You can view the minimal use case on this fiddle: https://jsfiddle.net/christophfriedrich/cm778wgj/14/
I suspect there might be a bug - shouldn't Vue recognize that objKeys
is used in the rendering of Modal
and update it accordingly? (The propagation of changes from obj
to objKeys
functions correctly.) If not, what am I missing and how can I achieve the desired outcome?