vue 组件生命周期执行顺序

先上一张Vue2.X官方文档中组件生命周期的图示:

vue 组件生命周期执行顺序_第1张图片
从上图能很清楚的看出单个组件的生命周期:
挂载: beforeCreate => created => beforeMount => mounted
更新: beforeUpdate => updated
销毁: beforeDestroy => destroyed

问:父子组件的生命周期执行顺序?

答:
挂载: parent beforeCreate => parent created => parent beforeMount => child beforeCreate => child created => child beforeMount => child mounted => parent mounted
更新: parent beforeUpdate => child beforeUpdate => child updated => parent updated
销毁: parent beforeDestroy => child beforeDestroy => child destroyed => parent destroyed

从以上能够看出:

  • 挂载时,子组件是在父组件before mount后开始挂载,并且子组件先mounted,父组件随后
  • 更新时,子组件是在父组件before update后开始更新,子组件先于父组件更新
  • 销毁时,子组件是在父组件before destroy后开始销毁,并且是子组件先销毁,父组件随后。

你可能感兴趣的:(vue,vue生命周期)