The changes are not reflected in DOM, even when the data is changing properly. Here is a very basic example to demonstrate the issue -
<template>
<input type="text" v-model="username" />
<p>{{error}}</p>
<button @click="saveData">Save</button>
</template>
<script>
export default {
data () {
model.error = ''; // adding a new property
return model; // 'model' is a global variable
}
methods: {
saveData() {
if (!this.username) {
this.error = 'Please enter the username!';
return;
}
// ... other code
}
}
};
</script>
After calling saveData() the error variable contains the message if username is not filled. But it's not showing up in the paragraph.
There is a trick. If I also change the username property when the error variable is changed, the changes are reflected.
datais used directly to create the$datafor the component. If you use the samemodelobject in multiple components they will all end up sharing the same$dataobject. That includes theerrorproperty. Is that really what you want? If it is then just add anerrorproperty to the initial definition ofmodelso that the property is made reactive when Vue first encounters that object.errorproperty only to this component. Can you show me how to do that?