初始化时
父组件-onBeforeMount
子组件-onBeforeMount
子组件-onMounted
父组件-onMounted
子组件更新时
子组件-onBeforeUpdate
子组件-onUpdated
父组件更新时-未影响子组件时
父组件-onBeforeUpdate
父组件-onUpdated
父组件更新时-影响子组件
父组件-onBeforeUpdate
子组件-onBeforeUpdate
子组件-onUpdated
父组件-onUpdated
组件销毁时
父组件-onBeforeUnmount
子组件-onBeforeUnmount
子组件-onUnmounted
父组件-onUnmounted
<template>
<div>HelloWorld</div>
<p>{{ state.title }}</p>
<button @click="changeTitle">change Title</button>
</template>
export default defineComponent({
name: "AddForm",
setup(props) {
console.log("props", props);
// 标题
let state = reactive({
title: `I'M Chilren`,
});
const changeTitle = () => {
console.log("changeTitle");
state.title = "Chilren Two";
};
return {
state,
changeTitle,
};
},
});
子组件AddForm.vue
<template>
<div>HelloWorld</div>
<p>{{ state.title }}</p>
<button @click="changeTitle">change Title</button>
</template>
<script lang="ts">
import {
defineComponent,
reactive,
onBeforeMount,
onMounted,
onBeforeUpdate,
onUpdated,
onBeforeUnmount,
onUnmounted,
} from "vue";
export default defineComponent({
name: "AddForm",
setup(props) {
console.log("props", props);
// 标题
let state = reactive({
title: `I'M Chilren`,
});
onBeforeMount(() => {
console.log("AddForm-onBeforeMount-");
});
onMounted(() => {
console.log("AddForm-onMounted-");
});
// DOM即将更新
onBeforeUpdate(() => {
console.log("AddForm-onBeforeUpdate-");
});
// DOM更新完毕
onUpdated(() => {
console.log("AddForm-onUpdated-");
});
// 即将销毁
onBeforeUnmount(() => {
console.log("AddForm-onBeforeUnmount-");
});
// 销毁完毕
onUnmounted(() => {
console.log("AddForm-onUnmounted-");
});
const changeTitle = () => {
console.log("changeTitle");
state.title = "Chilren Two";
};
return {
state,
changeTitle,
};
},
});
</script>
父组件AboutPage.vue
<template>
<div class="about">
<h1>This is an about page {{ state.title }}</h1>
<button @click="changeTitle">change About Title</button>
<AddForm :title="state.title" />
</div>
</template>
<script lang="ts">
import {
defineComponent,
reactive,
onBeforeMount,
onMounted,
onBeforeUpdate,
onUpdated,
onBeforeUnmount,
onUnmounted,
} from "vue";
import AddForm from "../components/AddForm/index.vue";
export default defineComponent({
name: "AboutPage",
components: {
AddForm,
},
setup(props) {
console.log("AboutPage", props);
let state = reactive({
title: "About",
});
onBeforeMount(() => {
console.log("AboutPage-onBeforeMount-Parent");
});
onMounted(() => {
console.log("AboutPage-onMounted-Parent");
});
// DOM即将更新
onBeforeUpdate(() => {
console.log("AboutPage-onBeforeUpdate-Parent");
});
// DOM更新完毕
onUpdated(() => {
console.log("AboutPage-onUpdated-Parent");
});
// 即将销毁
onBeforeUnmount(() => {
console.log("AboutPage-onBeforeUnmount-Parent");
});
// 销毁完毕
onUnmounted(() => {
console.log("AboutPage-onUnmounted-Parent");
});
const changeTitle = () => {
console.log("changeTitle-About");
state.title = "Chilren Two";
};
return {
state,
changeTitle,
};
},
});
</script>
父组件
子组件
子组件
<template>
<div>HelloWorld</div>
<p>{{ state.title }}</p>
<button @click="changeTitle">change Title</button>
</template>
<script lang="ts">
import {
defineComponent,
reactive,
onBeforeMount,
onMounted,
onBeforeUpdate,
onUpdated,
onBeforeUnmount,
onUnmounted,
} from "vue";
export default defineComponent({
name: "AddForm",
props: {
num: String,
title: String,
},
setup(props, context) {
console.log("props", props, context);
// 标题
let state = reactive({
title: `I'M Chilren`,
});
onBeforeMount(() => {
console.log("AddForm-onBeforeMount-");
});
onMounted(() => {
console.log("AddForm-onMounted-");
});
// DOM即将更新
onBeforeUpdate(() => {
console.log("AddForm-onBeforeUpdate-");
});
// DOM更新完毕
onUpdated(() => {
console.log("AddForm-onUpdated-");
});
// 即将销毁
onBeforeUnmount(() => {
console.log("AddForm-onBeforeUnmount-");
});
// 销毁完毕
onUnmounted(() => {
console.log("AddForm-onUnmounted-");
});
const changeTitle = () => {
console.log("changeTitle");
state.title = "Chilren Two";
context.emit("getSubTitle", "Chilren Two");
};
return {
state,
changeTitle,
};
},
});
</script>
父组件
<template>
<div class="about">
<h1>This is an about page {{ state.title }}</h1>
<button @click="changeTitle">change About Title</button>
<AddForm num="1" :title="state.title" @getSubTitle="getSubTitle" />
</div>
</template>
<script lang="ts">
import {
defineComponent,
reactive,
onBeforeMount,
onMounted,
onBeforeUpdate,
onUpdated,
onBeforeUnmount,
onUnmounted,
} from "vue";
import AddForm from "../components/AddForm/index.vue";
export default defineComponent({
name: "AboutPage",
components: {
AddForm,
},
setup(props) {
console.log("AboutPage", props);
let state = reactive({
title: "About",
});
onBeforeMount(() => {
console.log("AboutPage-onBeforeMount-Parent");
});
onMounted(() => {
console.log("AboutPage-onMounted-Parent");
});
// DOM即将更新
onBeforeUpdate(() => {
console.log("AboutPage-onBeforeUpdate-Parent");
});
// DOM更新完毕
onUpdated(() => {
console.log("AboutPage-onUpdated-Parent");
});
// 即将销毁
onBeforeUnmount(() => {
console.log("AboutPage-onBeforeUnmount-Parent");
});
// 销毁完毕
onUnmounted(() => {
console.log("AboutPage-onUnmounted-Parent");
});
const changeTitle = () => {
console.log("changeTitle-About");
state.title = "Chilren Two";
};
const getSubTitle = (value: string) => {
console.log("getSubTitle", value);
};
return {
state,
changeTitle,
getSubTitle,
};
},
});
</script>