<div id="outer" @click="fn">
<div @click="fn" id="inner">
click me
</div>
</div>
methods: {
fn() {
console.log('start');
setTimeout(() => {
console.log('timeout');
}, 0);
this.$nextTick(() => {
console.log('nextTick');
this.$nextTick(() => { console.log('nextTick2'); });
setTimeout(() => { console.log('timeout2'); });
});
console.log('end');
}
}
点击outer的时候,执行fn的结果如下,这个我可以理解,因为nextTick是基于promise实现的,优先级高于setTimeout
start
end
nextTick
nextTick2
timeout
timeout2
但是点击inner的时候,由于冒泡会执行两次fn,结果如下:
start
end
nextTick
nextTick2
start
end
nextTick
nextTick2
timeout
timeout2
timeout
timeout2
这个我不大明白,为什么不是
先执行完同步代码,
再执行优先级高一点的nextTick,
然后再执行setTimeout
start
end
start
end
nextTick
nextTick2
nextTick
nextTick2
timeout
timeout2
timeout
timeout2