How do I update props on a manually mounted vue component?
If you define an object outside of Vue and then use it in the data
for a Vue instance, it will be made reactive. In the example below, I use dataObj
that way. Although I follow the convention of using a data
function, it returns a pre-defined object (and would work exactly the same way if I'd used data: dataObj
).
After I mount the instance, I update dataObj.data
, and you can see that the component updates to reflect the new value.
const ComponentClass = Vue.extend({
template: '<div>Hi {{data}}</div>'
});
const dataObj = {
data: 'something'
}
let instance = new ComponentClass({
data() {
return dataObj;
}
});
// mount it
instance.$mount();
document.getElementById('target').appendChild(instance.$el);
setTimeout(() => {
dataObj.data = 'another thing';
}, 1500);
<script src="https://unpkg.com/vue@latest/dist/vue.js"></script>
<div id="target">
</div>