Vue.js bind object properties
Solution 1
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 adding contactNum
to your addr
object.
var vm = new Vue({
el: '#app',
data: {
addr: {
contactNum: "" // <-- this one
},
test: ""
}
});
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.
<input type="text" id="contactNum" @input="update(addr, 'contactNum', $event)" name="contactNum">
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.
var vm = new Vue({
el: '#app',
data: {
addr: {},
test: ""
},
methods: {
update: function(obj, prop, event) {
Vue.set(obj, prop, event.target.value);
}
}
});
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.
Solution 2
As per my comments, there are several things that you want to consider:
- The reason why your code is not working is due to the inherent inability of JS to watch for changes in object properties. This means that even though
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-Caveats - If you are going to have an arbitrary number of input fields, you are probably better of composing a custom input component, and simply use
v-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 new updateFormData
method, which is called by the component:
data: {
addrFields: ['contactNum', ...],
addr: {},
test: ""
},
methods: {
updateFormData: function(id, value) {
this.$set(this.addr, id, value);
}
}
We can still store your form data in the addr
object, which will be updated by the updateFormData
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 the addrField
as a prop using :id="addrField"
. We also want to make sure that we capture the custom-named updated
event emitted from within the component.
<my-input
v-for="(addrField, i) in addrFields"
:key="i"
:id="addrField"
v-on:inputUpdated="updateFormData"></my-input>
The template can look something like the following. It simply uses the id
prop for both its id
, name
, and placeholder
attribute (the latter for easy identification in the demo). We bind the @change
and @input
events, forcing it to trigger the updated
callback:
<script type="text/template" id="my-input">
<input
type="text"
:id="id"
:name="id"
:placeholder="id"
@input="updated"
@change="updated">
</script>
In the component logic, you let it know that it will receive id
as a prop, and that it should emit an inputUpdated
event using $.emit()
. We attach the ID and value as payloads, so that we can inform the parent what has updated:
var myInput = Vue.component('my-input', {
template: '#my-input',
props: {
id: {
type: String
}
},
methods: {
updated: function() {
this.$emit('inputUpdated', this.id, this.$el.value);
}
}
});
With the code above, we have a working example. In this case, I have created an arbirary array of input fields: contactNum
, a
, b
, and c
:
var myInput = Vue.component('my-input', {
template: '#my-input',
props: {
id: {
type: String
}
},
methods: {
updated: function() {
this.$emit('updated', this.id, this.$el.value);
}
}
});
var vm = new Vue({
el: '#app',
data: {
addrFields: ['contactNum', 'a', 'b', 'c'],
addr: {},
test: ""
},
methods: {
updateFormData: function(id, value) {
this.$set(this.addr, id, value);
}
}
});
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<div id="app">
<my-input
v-for="(addrField, i) in addrFields"
:key="i"
:id="addrField"
v-on:updated="updateFormData"></my-input>
<input type="text" id="test" v-model="test" name="test" placeholder="test">
<br/>
<strong>addr:</strong> {{addr}}<br/>
<strong>addr.contactNum:</strong> {{addr.contactNum}}<br />
<strong>test:</strong> {{test}}
</div>
<script type="text/template" id="my-input">
<input
type="text"
:id="id"
:name="id"
:placeholder="id"
@input="updated"
@change="updated">
</script>
Solution 3
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:
data: {
addr: {
contactNum: "" // <-- this one
},
test: ""
}
Sam YC
Updated on October 25, 2020Comments
-
Sam YC over 3 years
Why I can't bind the object properties in Vue? The object
addr
is not reactive immediately, buttest
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: "" } });
-
Terry over 6 yearsRefer to the change detection caveats section of VueJS docs. You are running into this issue because
addr
is reactive butaddr.contactNum
is not, since it is not declared when the component is mounted. There is no other way (due to limitations in JS itself) than to explicitly declare it, either when initializing thedata
object or usingVue.set()
. Even though you may have arbitrary number of inputs, you will know which key they will be referring to in theaddr
object, so you can use that inVue.set()
. -
Terry over 6 yearsAn alternative way is actually to compose a generic input element, say
<my-input-component>
with its ownv-model
mapping, and you simply include them in your app as a component. These components can emit the updated values to the parent (app in this case), so the parent will always have the updated values of all inputs in itself. -
LiranC over 6 yearsby the way, in my enviroment it works just fine. i have copied the code to the generated project by
vue-cli
. i am also using vue 2.5.2
-
-
Sam YC over 6 yearsoh no, got better way? I have a lot of property for
addr
object, I don't want to initialize them one by one. -
Mat J over 6 yearsYou can assign addr itself with new object and it will be setup correctly.
-
Sam YC over 6 yearsgot better way? I have lot of properties, prefer not to initialize one by one.
-
Sam YC over 6 years@MatJ What do you mean assign a new object? Empty object?
-
haMzox over 6 yearsYes, you can set Vue properties with Vue.set outside vue scope.
-
Andrzej Moroz over 3 yearsYOU CAN TRY TO CREATE SEPARATE COMPONENT :)