|
@@ -0,0 +1,120 @@
|
|
|
+<template>
|
|
|
+ <div>
|
|
|
+ <h1>生命周期</h1>
|
|
|
+ <p>{{ msg }}</p>
|
|
|
+ <button @click="changeMain">更改</button>
|
|
|
+
|
|
|
+ </div>
|
|
|
+
|
|
|
+</template>
|
|
|
+
|
|
|
+
|
|
|
+<script setup>
|
|
|
+/**
|
|
|
+ * vue2生命周期三个阶段
|
|
|
+ * 初始:beforeCreate created
|
|
|
+ * 运行:beforeMount mounted beforeUpdate updated
|
|
|
+ * 销毁:beforeDestroy destroyed
|
|
|
+ *
|
|
|
+ * vue3生命周期
|
|
|
+ * 初始:setup
|
|
|
+ * 运行:
|
|
|
+ * 挂载:onBeforeMount onMounted
|
|
|
+ * 更新:onBeforeUpdate onUpdated
|
|
|
+ * 卸载: onBeforeUnMount onUnMounted
|
|
|
+ */
|
|
|
+
|
|
|
+ import {ref,onBeforeMount,onMounted,onBeforeUpdate,onUpdated,onBeforeUnmount,onUnmounted} from 'vue';
|
|
|
+
|
|
|
+let msg = ref("哈哈")
|
|
|
+
|
|
|
+function changeMain() {
|
|
|
+ msg.value = '嘻嘻'
|
|
|
+}
|
|
|
+ onBeforeMount(()=>{
|
|
|
+ console.log("挂载前")
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ onMounted(()=>{
|
|
|
+ console.log("挂载后")
|
|
|
+ })
|
|
|
+
|
|
|
+ onBeforeUpdate(()=>{
|
|
|
+ console.log("onBeforeUpdate")
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ onUpdated(()=>{
|
|
|
+ console.log("onUpdated")
|
|
|
+ })
|
|
|
+
|
|
|
+ onBeforeUnmount(()=>{
|
|
|
+ console.log("onBeforeUnMount")
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ onUnmounted(()=>{
|
|
|
+ console.log("onUnMounted")
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// export default {
|
|
|
+// // data() {
|
|
|
+// // return {
|
|
|
+// // msg: "你好",
|
|
|
+// // };
|
|
|
+// // },
|
|
|
+// // beforeCreate() {
|
|
|
+// // console.log("创建前");
|
|
|
+// // },
|
|
|
+// // created() {
|
|
|
+// // console.log("创建后");
|
|
|
+// // },
|
|
|
+// // beforeMount() {
|
|
|
+// // console.log("挂载前", this.$el, document.querySelector("p"));
|
|
|
+// // },
|
|
|
+// // mounted() {
|
|
|
+// // console.log("挂载后", this.$el, document.querySelector("p"));
|
|
|
+// // },
|
|
|
+// // beforeUpdate() {
|
|
|
+// // console.log("更新前", this.$el, document.querySelector("p").innerText);
|
|
|
+// // },
|
|
|
+// // updated() {
|
|
|
+// // console.log("更新后", this.$el, document.querySelector("p").innerText);
|
|
|
+// // },
|
|
|
+// // beforeDestroy() {
|
|
|
+// // console.log("销毁前");
|
|
|
+// // },
|
|
|
+// // destroyed() {
|
|
|
+// // console.log("销毁后");
|
|
|
+// // },
|
|
|
+// // methods: {
|
|
|
+// // changeMain() {
|
|
|
+// // this.msg = "哈哈哈";
|
|
|
+// // },
|
|
|
+// // },
|
|
|
+
|
|
|
+// setup() {
|
|
|
+// return {
|
|
|
+
|
|
|
+// }
|
|
|
+// },
|
|
|
+// };
|
|
|
+</script>
|
|
|
+
|
|
|
+<style lang="scss" scoped></style>
|