admin管理员组文章数量:1315033
I'm working on a single-page app with Vue.js and its official router.
I have a menu and a ponent (.vue file) per every section which I load using the router. In every ponent I have some code similar to this:
<template>
<div> <!-- MY DOM --> </div>
</template>
<script>
export default {
data () {},
methods: {},
route: {
activate() {},
},
ready: function(){}
}
</script>
I want to execute a piece of code (init a jQuery plugin) once a ponent has finished transitioning in. If I add my code in the ready
event, it gets fired only the first time the ponent is loaded. If I add my code in the route.activate
it runs every time, which is good, but the DOM is not loaded yet, so is not possible to init my jQuery plugin.
How can I run my code every time a ponent has finished transitioning in and its DOM is ready?
I'm working on a single-page app with Vue.js and its official router.
I have a menu and a ponent (.vue file) per every section which I load using the router. In every ponent I have some code similar to this:
<template>
<div> <!-- MY DOM --> </div>
</template>
<script>
export default {
data () {},
methods: {},
route: {
activate() {},
},
ready: function(){}
}
</script>
I want to execute a piece of code (init a jQuery plugin) once a ponent has finished transitioning in. If I add my code in the ready
event, it gets fired only the first time the ponent is loaded. If I add my code in the route.activate
it runs every time, which is good, but the DOM is not loaded yet, so is not possible to init my jQuery plugin.
How can I run my code every time a ponent has finished transitioning in and its DOM is ready?
Share Improve this question asked Jul 13, 2016 at 23:25 Agu DondoAgu Dondo 13.6k7 gold badges62 silver badges71 bronze badges3 Answers
Reset to default 2As you are using Vue.js Router, it means that each time you will transition to a new route, Vue.js will need to update the DOM. And by default, Vue.js performs DOM updates asynchronously.
In order to wait until Vue.js has finished updating the DOM, you can use Vue.nextTick(callback). The callback will be called after the DOM has been updated.
In your case, you can try:
route: {
activate() {
this.$nextTick(function () {
// => 'DOM loaded and ready'
})
}
}
For further information:
- https://vuejs/api/#Vue-nextTick
- https://vuejs/guide/reactivity.html#Async-Update-Queue
You can use
mounted(){
// jquery code
}
Though this may be a bit late... If I guess correctly, the ponent having problem stays on the page when you navigate between routes. This way, Vue reuses the ponent, changing what's inside it that needs to change, rather than destroy and recreate it. Vue provides a key
attribute to Properly trigger lifecycle hooks of a ponent
. By changing a ponent's key
, we can indicate Vue to rerender it. See key in guide for details and key in api for code sample.
本文标签: javascriptVuejs Router Run code when component is readyStack Overflow
版权声明:本文标题:javascript - Vue.js Router: Run code when component is ready - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741973256a2407965.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论