Why I can't bind the object properties in Vue? The object addr
is not reactive immediately, but test
is reactive, how come? In this case, how should I bind it?
HTML
<div id="app">
<input type="text" id="contactNum" v-model="addr.contactNum" name="contactNum">
<input type="text" id="test" v-model="test" name="test">
<br/>
{{addr}}<br/>
{{addr.contactNum}}<br/>
{{test}}
</div>
Javascript
var vm = new Vue({
el: '#app',
data: {
addr: {},
test: ""
}
});
During initialisation Vue sets up getters and setters for every known property. Since
contactNum
isn't initially set up, Vue doesn't know about that property and can not update it properly. This can be easly fixed by addingcontactNum
to youraddr
object.The above is called reactivity in Vue. Since Vue doesn't support adding properties dynamically to its reactivity system, we may need some kind of workaround. A possible solution is provided by the API. In case of dynamically added properties we can use
Vue.set(vm.someObject, 'b', 2)
.Doing so the markup would need to get some update. Instead of using
v-model
it'd be better to use an event listener like@input
. In this case our markup could look like this.So basically the function will get triggered every time the input elements value changes. Obviously doing so will also require some adjustments on the JS part.
Since Vue triggers
Vue.set()
on any reactive element, we simply call it on our own because Vue doesn't recognizes a dynamically added property as a reactive one. Of course, this is only one possible solution and there may be lots of other workarounds. A fully working example can be seen here.Edit your Vue
data
with this since it's getter and setter methods are not set up. Also, check out Declarative Reactive Rendering on Vue docs here:As per my comments, there are several things that you want to consider:
addr
is reactive, any properties added toaddr
that is not done when it is declared will make it non-reactive. Refer to the VueJS docs for more details: https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveatsv-for
to iteratively inject input fields based on the number of input fields you have.Now back to the second point, if you know what fields
addr
will have, you can simply declare it in your app. We create a newupdateFormData
method, which is called by the component:We can still store your form data in the
addr
object, which will be updated by theupdateFormData
method based on the received payload using.$set()
. Now, we can then create a custom Vue component for your input element.In the example below, the component will iterate through all your
addrFields
, and pass down theaddrField
as a prop using:id="addrField"
. We also want to make sure that we capture the custom-namedupdated
event emitted from within the component.The template can look something like the following. It simply uses the
id
prop for both itsid
,name
, andplaceholder
attribute (the latter for easy identification in the demo). We bind the@change
and@input
events, forcing it to trigger theupdated
callback:In the component logic, you let it know that it will receive
id
as a prop, and that it should emit aninputUpdated
event using$.emit()
. We attach the ID and value as payloads, so that we can inform the parent what has updated:With the code above, we have a working example. In this case, I have created an arbirary array of input fields:
contactNum
,a
,b
, andc
: