admin管理员组文章数量:1183730
I want to access the inner <input>
element after it has been created by Vue:
<li v-repeat="subtask: subtasks">
<input v-model="subtask.name" type="text">
</li>
However, this code does not work:
/* add a new item (that in turn will create a new DOM node) */
subtasks.push({
name: 'wash the dishes',
id: 'box-123'
});
/* ... Around now, Vue should get busy creating the new <li> ... */
/* Now the element should exist, but it doesn't */
console.log(document.getElementById('box-123'));
// --> null
However, the getElementById
call comes back empty handed--the node does not exist at that time.
When can I be sure Vue has created / updated the DOM?
I want to access the inner <input>
element after it has been created by Vue:
<li v-repeat="subtask: subtasks">
<input v-model="subtask.name" type="text">
</li>
However, this code does not work:
/* add a new item (that in turn will create a new DOM node) */
subtasks.push({
name: 'wash the dishes',
id: 'box-123'
});
/* ... Around now, Vue should get busy creating the new <li> ... */
/* Now the element should exist, but it doesn't */
console.log(document.getElementById('box-123'));
// --> null
However, the getElementById
call comes back empty handed--the node does not exist at that time.
When can I be sure Vue has created / updated the DOM?
Share Improve this question asked Apr 6, 2015 at 12:25 BryceBryce 6,61010 gold badges42 silver badges64 bronze badges3 Answers
Reset to default 28Vue batches DOM updates and performs them asynchronously for performance reasons. After a data change, you can use Vue.nextTick
to wait for the DOM updates to finish:
subtasks.push({});
Vue.nextTick(function () {
// DOM updated
});
If you are using it inside a component method where this
is available (and a reference to the component), you can use:
this.$nextTick(function () {
// DOM updated
});
References:
Vue.nextTick
vm.$nextTick
If vue does not have a callback for this you can use a MutationObserver listener on the parent: https://developer.mozilla.org/en/docs/Web/API/MutationObserver
This seem to work for me
mounted() {
window.addEventListener('load', (event) => {
...
});
},
本文标签: javascriptHow to determine when Vue has finished updating the DOMStack Overflow
版权声明:本文标题:javascript - How to determine when Vue has finished updating the DOM? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738301524a2073631.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论