在用vue动态修改属性值的环节中,遇到了关于函数的执行顺序问题,因为业务的不同,尝试用回调、.then、和同步执行,统统踩在坑里。。。
var widthNum;
var heightNum;
document.onmousemove = function(e){
oDrag.style.width = e.pageX - disx-195+'px';
oDrag.style.height = e.pageY - disy-85+'px';
widthNum = e.pageX - disx-195;
heightNum = e.pageY - disx-85;
}
var i = 0;
console.log(this);
this.$emit('changeSize',{theId:i,width:widthNum,height:heightNum});
console.log(widthNum);
document.onmouseup = function(e){
document.onmousemove = document.onmouseup = null;
}
原来是这么写的,问题很明显,我每次拿到的widthNum都不是最新生成的,而是上面定义的,拿到的是空值(undefined).
问题实际上出在上面的onmousemove函数没有执行之前就会先执行后面的$emit函数,导致拿不到我们想要的值;
解决这个问题,最直接的想法可能是1.用一个判断&#x