Vue.nextTick( [callback, context] )

1、在下次 DOM 更新循环结束之后执行延迟回调。在修改数据之后立即使用这个方法,获取更新后的 DOM。

Vue.nextTick(() => {})  /  this.$nextTick(() => {// 更新完成})

<template lang="html">
   <div>
       <span>{{msg}}</span>
   </div>
</template>

<script>
export default {
    data () {
        return {
            msg: '没有更新之前'
        }
    },
    methods: {
        updateMsg () {
            this.msg = '更新完成'
            console.log('aaa', this.$el.textContent) // 没有更新之前
            this.$nextTick(() => {
                console.log('bb', this.$el.textContent) // 更新完成
            })
        }
    },
    mounted () {
        this.updateMsg()
    }
}
</script>

<style lang="css">
</style>

 

posted @ 2017-09-26 14:50  浮生如梦似离殇  阅读(1051)  评论(0编辑  收藏  举报