组件部分
<template>
<div>
<h1>Hello World!</h1>
<!-- 点击时更新 -->
<button @click="num++">{{ num }}</button>
</div>
</template>
<script>
export default {
data() {
return {
num: 1
}
},
beforeCreate() {
console.log('beforeCreate:创建前--备孕')
},
created() {
console.log('created:创建完毕--怀孕')
},
beforeMount() {
console.log('mounted:准备出生--将要添加到页面')
},
mounted() {
console.log('mounted:出生--显示到页面')
},
beforeUpdate() {
console.log('beforeUpdate:将要更新')
},
updated() {
console.log('updated:更新完毕')
},
beforeDestroy() {
console.log('destroyed:将要销毁--快死了')
},
destroyed() {
console.log('destroyed:销毁完毕--死了')
}
}
</script>
<style lang="scss" scoped>
</style>
App.vue
<template>
<div>
<!-- 生命周期 组件从生到死的过程 -->
<!-- 如果点击后把show改成了true就显示,反之不显示 -->
<button @click="show = true">添加</button>
<button @click="show = false">移除</button>
<!-- 先在data设定它为false,打开时就是不显示的 -->
<hello-world v-if="show" />
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
components: { HelloWorld },
data() {
return {
show: false
}
}
}
</script>
<style lang="scss" scoped>
</style>
![在这里插入图片描述](https://img-blog.csdnimg.cn/6afa633092cf4dd28b0baa0b54eea11c.png)