What is the reason why alert pops up in the following order in created and watch in the following code in vue?

the following is part of the code in a child component. When assigning a value to amount in the parent component, why the alert pop-up order is" created" = > "init: 1" = > 1 = > "init: 1" = > 2 = > init: 2"= > "init: 2"
data () {

return {
  currentLimitType : ""
}

},
watch: {

amount(val) {
  alert(1)
  if (this.currentLimitType === "") {
    this.initData()
  }
  alert(2)
  ... // 

},
created () {

alert("created")
this.initData()

},
methods: {

async initData() {
  alert("init: 1")
  const res = await queryOnceLimit() // ajax
  alert("init: 2")
  this.handlInitData(res, () => {
    this.computedCurrentLimitType() // currentLimitType
    this.$emit("moneyLimitOk")
  })
},
Aug.27,2021

execute created, first and then watch


component must first created , and then parse the value passed by the parent component
here you should use mounted


this should have nothing to do with created and watch . Your confusion is all because the async initData () method is asynchronous.
you remove the asynchronous operations and execute created and watch and you can clearly see the execution sequence.

besides, you didn't say what you wanted to see? is your question out of order?

< hr >

update, although the business scenario you gave is not clear enough. But you can also analyze the whole process:

  1. parent component created finishes executing created , pops up alert ('created') . Go to the initData method of async/await at the same time.
  2. The initData method enters, and triggers alert ('init: 1') . At the same time, due to the asynchronous waiting feature of await , the method below will be suspended, and the execution of will continue only after waiting for its ajax request to return.
  3. has just finished creating the subcomponent, go to the method, because the program is suspended asynchronously, the program will not be idle and keep going. After mounting the (mounted) child component, mount the parent component (you said you changed the value in the parent component through refs), so trigger the watch , of the child component, that is, pop up alert (1) .
  4. look at your sentence to judge if (this.currentLimitType =') {this.initData ()} you can see that you have entered the initData function again, and will execute alert ('init: 1') again. As you encounter asynchrony again, then continue to execute alert (2) .
  5. the asynchronous request before this time successfully returns data, and pops up alert ('init: 2') . Then there is another request that has not been returned, and you can continue to wait. The process is the same as above. This is probably the process, and the connection is like this:
created => init: 1 => 1 => init: 1 => 2 => init: 2
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3cd2f-2c326.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3cd2f-2c326.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?