Vue nextTick
2021-06-03 18:12
316 查看
vm.$nextTick( callback] )
updated执行的频率太高了,希望页面更新之后执行的逻辑,其实
nextTick更合适
1.作用:
数据改变
并且dom
更新之后执行的回调函数- 执行时机:将回调延迟到下次 DOM 更新循环之后执行。
- 使用方法:在修改数据之后立即使用它,然后等待 DOM 更新。
new Vue({ // ... methods: { // ... example: function () { // 修改数据 this.message = 'changed' // DOM 还没有更新 this.$nextTick(function () { // DOM 现在更新了 // `this` 绑定到当前实例 this.doSomethingElse() }) } } })
demo
<template> <div> <h2>update的缺点和解决方案nextTick</h2> <input type = "text" v-model = "msg"/> <br/> <input type = "text" v-model = "info"/> <h2 ref = "info">info:{{ info }}</h2> <button @click = "changeInfo">修改info</button> </div> </template> <script> export default { updated() { // 任何数据更新都会触发updated // 执行频率很高 // 开发中 用的不是很多 // console.log('执行啦!') }, data() { return { msg: '感觉自己萌萌哒!', info: '阿珍爱上了阿强', }; }, methods: { changeInfo() { // 修改数据 this.info = '在一个没有星星的夜晚!'; // 页面还没更新 console.log('innerHTML:', this.$refs.info.innerHTML); // 数据改变, dom更新完毕 this.$nextTick(() => { // 绑定this,更新之后的DOM console.log('nextTick-innerHTML:', this.$refs.info.innerHTML); }); }, }, }; </script> <style></style>
相关文章推荐
- Vue.nextTick()的正确使用
- Vue--Vue.nextTick()的使用
- 关于 Vue 中 vm.$nextTick、vm.$set 的使用和理解
- 什么时候需要用的Vue.nextTick()
- Vue中的$.nextTick的理解
- vue 的this.$nextTick(()=>{})作用
- 在vue中操作DOM--this.$nextTick()
- vue 笔记之this指向问题以及$nextTick()的用法
- vue:this.nextTick()的使用
- vue(1) -- this.$nextTick
- Vue源码小问答二:Vue.nextTick的用法
- Vue.nextTick( [callback, context] )
- Vue.nextTick( )
- Vue.nextTick()函数
- Vue中 等待DOM或者数据完成 在执行 --this.$nextTick()
- vue 中 $nextTick用法
- vue nextTick深入理解-vue性能优化、DOM更新时机、事件循环机制
- Vue源码解析之nextTick
- vue基础(五):给DOM添加事件的特殊情况 $nextTick
- vue 初始化为dom元素添加事件 vm.$nextTick( [callback] )