Vue中nextTick()解析
最近,在开发的时候遇到一个问题,让我对vue中nextTick()的用法加深了了解~
下面是在组件中引用的一个拖拽的组件:
<vue-draggable-resizable
class="drag-img"
:w="coordinate[0].width"
:h="coordinate[0].height"
:x="coordinate[0].abs"
:y="coordinate[0].ord"
@dragging="onDragAvator"
@resizing="onResizeAvator"
@dragstop="onDragstopAvator"
@onDragStart="onDragStartAvator"
:min-width="50"
:min-height="50"
:handles="['tl','tr','br','bl']"
:lock-aspect-ratio="true"
:parent="true">
<img @click="setAvatorDafault" src="@/assets/img/icon_touxiang@2x.png" alt="">
</vue-draggable-resizable>
这个拖拽组件的横坐标和纵坐标、宽高是由data的一个数据控制的:
data() {
return {
coordinate:[{
width: 50,
height: 50,
abs: 10,
ord: 10
}]
}
}
在dragging和resizing的过程中,这个数据和dom的操作应该都是双向绑定的:
onResizeAvator: function (x, y, width, height) {
this.coordinate[0].abs = x
this.coordinate[0].ord = y
},
onDragAvator: function (x, y) {
this.coordinate[0].abs = x
this.coordinate[0].ord = y
},
但是,在编辑回显的时候,需要将异步获取的数据赋给coordinate,再显示到页面。
现在问题来了,当数据coordinate被赋值成异步获取的数据后,页面中的拖拽组件的宽高并没有变化,这是为什么?
methods: {
getDefaultData() {
let that = this
axios.get(this.$store.state.marketinghost
+ '/fission/task/get/bycode?onlischoolCode=' + this.onlischoolCode
+ '&taskCode=' + this.$route.query.id)
.then(res => {
if(res.data.code == "1") {
var data = res.data.data
if (data.components.length) {
that.coordinate = data.components
}
}
})
.catch((err) => {
Promise.resolve(err);
})
}
}
mounted() {
this.getDefaultData()
}
加了一个变量控制拖拽组件后,组件DOM会强制性更新,宽高就变成回显时候的值了
<vue-draggable-resizable
class="drag-img"
v-if="!editLoading"
:w="coordinate[0].width"
:h="coordinate[0].height"
:x="coordinate[0].abs"
:y="coordinate[0].ord"
@dragging="onDragAvator"
@resizing="onResizeAvator"
@dragstop="onDragstopAvator"
@onDragStart="onDragStartAvator"
:min-width="50"
:min-height="50"
:handles="['tl','tr','br','bl']"
:lock-aspect-ratio="true"
:parent="true">
<img @click="setAvatorDafault" src="@/assets/img/icon_touxiang@2x.png" alt="">
</vue-draggable-resizable>
data() {
return {
editLoading: false,
coordinate:[{
width: 50,
height: 50,
abs: 10,
ord: 10
}]
}
}
methods: {
getDefaultData() {
let that = this
that.editLoading = true
axios.get(this.$store.state.marketinghost
+ '/fission/task/get/bycode?onlischoolCode=' + this.onlischoolCode
+ '&taskCode=' + this.$route.query.id)
.then(res => {
if(res.data.code == "1") {
var data = res.data.data
if (data.components.length) {
that.coordinate = data.components
that.$nextTick(() => {
that.editLoading = false
})
}
}
})
.catch((err) => {
Promise.resolve(err);
})
}
}
mounted() {
this.getDefaultData()
}
为了更加了解nextTick(),下面是vue官网关于异步更新队列的解释:
https://cn.vuejs.org/v2/guide/reactivity.html#%E5%BC%82%E6%AD%A5%E6%9B%B4%E6%96%B0%E9%98%9F%E5%88%97
总之,当你设置 vm.someData = 'new value',该组件不会立即重新渲染。当刷新队列时,组件会在下一个事件循环“tick”中更新,为了在数据变化之后等待 Vue 完成更新 DOM,可以在数据变化之后立即使用 Vue.nextTick(callback) :
Vue.component('example', {
template: '<span>{{ message }}</span>',
data: function () {
return {
message: '未更新'
}
},
methods: {
updateMessage: function () {
this.message = '已更新'
console.log(this.$el.textContent) // => '未更新'
this.$nextTick(function () {
console.log(this.$el.textContent) // => '已更新'
})
}
}
})
因为nextTick()返回的事一个Promise对象,所以也可以写成async/await的方式:
methods: {
updateMessage: async function () {
this.message = '已更新'
console.log(this.$el.textContent) // => '未更新'
await this.$nextTick()
console.log(this.$el.textContent) // => '已更新'
}
}
Vue中nextTick()解析的更多相关文章
- vue中nextTick
vue中nextTick可以拿到更新后的DOM元素 如果在mounted下不能准确拿到DOM元素,可以使用nextTick 在Vue生命周期的created()钩子函数进行的DOM操作一定要放在Vue ...
- Vue中$nextTick的理解
Vue中$nextTick的理解 Vue中$nextTick方法将回调延迟到下次DOM更新循环之后执行,也就是在下次DOM更新循环结束之后执行延迟回调,在修改数据之后立即使用这个方法,能够获取更新后的 ...
- 通俗易懂了解Vue中nextTick的内部实现原理
1. 前言 nextTick 是 Vue 中的一个核心功能,在 Vue 内部实现中也经常用到 nextTick.在介绍 nextTick 实现原理之前,我们有必要先了解一下这个东西到底是什么,为什么要 ...
- vue中nextTick的理解
A. vue 中的 nextTick 是什么? 1.首先需要清楚,nextTick是一个函数:这个函数的作用,简单理解就是下一次渲染后才执行 nextTick 函数中的操作: 2.在下一次 DOM 更 ...
- Vue中v-model解析、sync修饰符解析
上善若水,水善利萬物而不爭.——<道德經> 简介 在平时开发是经常用到一些父子组件通信,经常用到props.vuex等等,这里面记录另外的三种方式v-model.sync是怎么使用,再说是 ...
- vue中$nextTick详细讲解保证你一看就明白
1.功能描述 今天我们要实现这个一个小功能: 页面渲染完成后展示一个div元素: 当点击这个div元素后: div元素消失: 出现一个input元素:并且input元素聚焦 想必大家我觉得简单,我们一 ...
- vue中$nextTick的使用
转载 https://www.jb51.net/article/154823.htm ,写的通俗易懂 在这里我有一个疑问,因为在vue中mounted里面执行后,dom节点是挂载上去了的,所以视图上 ...
- vue中nextTick的使用(转载)
转载自:https://www.cnblogs.com/chaoyuehedy/p/8985425.html 简介 vue是非常流行的框架,他结合了angular和react的优点,从而形成了一个轻量 ...
- vue中$nextTick的用法
简介 vue是非常流行的框架,他结合了angular和react的优点,从而形成了一个轻量级的易上手的具有双向数据绑定特性的mvvm框架.本人比较喜欢用之.在我们用vue时,我们经常用到一个方法是th ...
随机推荐
- 在vmware中 centos7安装gooderp
环境为windows 10系统,vmware 12,centos 7.4.centos安装了gnome桌面,用里面的终端来安装,自带的firefox浏览器. 增加用户 首先要新建一个用户来管理good ...
- 在Eclipse中设置Maven插件
[步骤] Maven插件的设置: ①installations:指定Maven核心程序的位置.不建议使用Maven插件自带的Maven程序,而应该使用我们自己解压的那个. ②user settings ...
- Java基础学习总结(85)——Java中四种线程安全的单例模式实现方式
- python 线程进程
一 线程的2种调用方式 直接调用 实例1: import threading import time def sayhi(num): #定义每个线程要运行的函数 print("runni ...
- Linear and Logistic Regression in TensorFlow
Linear and Logistic Regression in TensorFlow Graphs and sessions TF Ops: constants, variables, funct ...
- RMI分布式议程服务学习
转自:http://6221123.blog.51cto.com/6211123/1112619 这里讲述的是基于JDK1.5的RMI程序搭建,更简单的说是一个 HelloWorld RMI. 1. ...
- 【页面传值6种方式】- 【JSP 页面传值方法总结:4种】 - 【跨页面传值的几种简单方式3种】
阅读目录 1. URL 链接后追加参数 2. Form 3. 设置 Cookie 4. 设置 Session JSP 页面间传递参数是项目中经常需要的,这应该算是 web 基本功吧. 试着将各种方式总 ...
- 在代码动态设置RelativeLayout的属性,比如layout_below
( (RelativeLayout.LayoutParams)holder.ivLvDivider.getLayoutParams()).addRule(RelativeLayout.BELOW, R ...
- Hero HDU4310 贪心
When playing DotA with god-like rivals and pig-like team members, you have to face an embarrassing s ...
- 马悦:《Linux内核分析》MOOC课程
http://www.cnblogs.com/20135235my/p/5237267.html