本篇將探討關於Vue中父子組件的生命週期順序。bash
父組件:ui
<template>
<div id="parent">
<child></child>
</div>
</template>
<script>
import child from './components/child'
export default {
name: 'parent',
components: {
child
},
beforeCreate() {
console.log('I am parents beforeCreated');
},
created() {
console.log('I am parents created');
},
beforeMount() {
console.log('I am parents beforeMount');
},
mounted() {
console.log('I am parents mounted');
}
}
</script>
複製代碼
子組件:spa
<template>
<div class="child">
child
</div>
</template>
<script>
export default {
name: 'child',
beforeCreate() {
console.log('I am child beforeCreated');
},
created() {
console.log('I am child created');
},
beforeMount() {
console.log('I am child beforeMount');
},
mounted() {
console.log('I am child mounted');
}
}
</script>
複製代碼
執行結果: code
咱們從而能夠得出父子組件的執行順序爲:component
注意:cdn