I've been developing a generic Field component for use in forms in a Vue web app I'm developing. In my EditProduct component, for example, I have multiple instances of the Field component, some of them of type 'checkbox', some 'text', etc. Effectively what the checkbox variant does is generate something like this:
<div class="col-xs-2">
<label>
<input id='organic' name='organic' type="checkbox" v-model="product.organic"/> Organic
</label>
</div>
from this:
<field :cols="2" name="organic" v-model="product.organic" type="checkbox"></field>
That's much simplified, but shows the general idea. What I'd like to be able to do is to pass something like a 'change' handler function name as a prop, like this:
<field :cols="2" name="organic" v-model="product.organic" type="checkbox" change="this.handleSomething"></field>
This would call a function in the EditProduct component. I'm not sure how to get this to work, though. I already have a change handler method in my Field component which is there to emit the notification event:
methods: {
handleCheckbox: function (event) {
this.$emit('input', event.target.checked);
if(this.change){
//do something here...
}
}
},
My idea was to test for the existence of a 'change' prop here and then do something, but all I have at this point is a string containing the name of a function. How do I actually use this to call a function, preferably with an argument derived from $event (such as $event.target.checked, say)?