On the execution order of flushSchedulerQueue and nextTick in Vue Source Code

first take a look at the code example:

<template>
  <div ref="box">{{content}}</div>
</template>

<script>
export default {
  data() {
    return {
      content: "before"
    }
  },
  mounted() {
    this.nextTick(() => {
      console.log(this.$refs.box.innerHTML) // after
    })
    this.content = "after"
    this.nextTick(() => {
      console.log(this.$refs.box.innerHTML) // after
    })
  }
}
</script>

I have looked at the source code of Vue again these two days. NextTick (cb) will put cb push into callbacks, and this.content = "after" will also flushSchedulerQueue push the execution function of watcher dependent on content to callbacks, and then flushCallbacks after the synchronization code has been executed. In that case, when the first console.log (this.$refs.box.innerHTML) in the example is executed, the content is not updated, so why output after?

< hr >

to update, if I introduce Vue through the script tag, the output is before and after, but if it is run through a webpack build, the output is after.

Jul.11,2022

your first is after ?
https://jsfiddle.net/63180768...


I also encountered the same problem as the landlord. The local webpack packaged dev server,vue version 2.6.11.

Menu