水中牛 發表於 2023-8-11 22:58:00

vue2生命周期、vue3生命周期、uni-app生命周期

<h2>一、生命周期</h2>
<p>Vue 实例有一个完整的生命周期。</p>
<p>也就是从开始创建、初始化数据、编译模版、挂载 Dom -&gt; 渲 染、更新 -&gt; 渲染、卸载等一系列过程,我们称这是 Vue 的生命周期</p>
<h2>二、vue2生命周期(钩子函数)</h2>
<h3>(1)钩子函数:</h3>
<ul>
<li>在特定阶段或特定事件发生时被执行的函数</li>
<li>在钩子函数中可以编写逻辑
<ul>
<li>【初始化操作、发送请求、处理DOM、注册事件、挂载之前请求数据、在更新后执行一些额外的逻辑】</li>
</ul>
</li>
</ul>
<h3>(2)常用钩子函数:</h3>
<table border="0">
<tbody>
<tr>
<td>beforeCreate</td>
<td>组件实例被创建之初,组件的属性生效之前</td>
</tr>
<tr>
<td>created</td>
<td>组件实例已经完全创建,属性也绑定,但真实 dom 还没有生成,$el 还不可用</td>
</tr>
<tr>
<td>beforeMount</td>
<td>在挂载开始之前被调用:相关的 render 函数首次被调用</td>
</tr>
<tr>
<td>mounted</td>
<td>el 被新创建的 vm.$el 替换,并挂载到实例上去之后调用该钩子【可多次调用】</td>
</tr>
<tr>
<td>beforeUpdate</td>
<td>组件数据更新之前调用,发生在虚拟 DOM 打补丁之前,即在重新渲染之前进行最后的准备工</td>
</tr>
<tr>
<td>updated</td>
<td>组件数据更新之后,重新渲染完成后。此阶段表示Vue实例已经完成了数据更新,并已经重新渲染到DOM</td>
</tr>
<tr>
<td>beforeDestory</td>
<td>组件销毁前调用,此阶段用于清理工作,如移除事件监听器或取消定时器。</td>
</tr>
<tr>
<td>destoryed</td>
<td>组件销毁后调用,此阶段表示Vue实例已经完全销毁,所有的事件监听器和组件实例都已被移除。</td>
</tr>
<tr>
<td>activited</td>
<td>keep-alive 专属,组件被激活时调用</td>
</tr>
<tr>
<td>deadctivated</td>
<td>keep-alive 专属,组件被销毁时调用</td>
</tr>
</tbody>
</table>
<div style="text-align: center">用 keep-alive 包裹的组件在切换时不会进行销毁,而是缓存到内存中并执行 deactivated 钩子函数,命中缓存渲染后会执行 activated 钩子函数</div>
<div style="text-align: left">
<h3>(3)包含子组件的父组件从创建到销毁过程中,Vue子组件和父组件生命周期的执行顺序</h3>
</div>
<h5 class="md-end-block md-heading md-focus" style="text-align: left"><span class="md-expand">1)加载渲染过程:</span></h5>
<ol class="ol-list" start="">
<li class="md-list-item">
<p class="md-end-block">父组件 beforeCreate</p>
</li>
<li class="md-list-item">
<p class="md-end-block">父组件 created</p>
</li>
<li class="md-list-item">
<p class="md-end-block">父组件 beforeMount |</p>
</li>
<li class="md-list-item">
<p class="md-end-block">子组件 beforeCreate</p>
</li>
<li class="md-list-item">
<p class="md-end-block">子组件 created</p>
</li>
<li class="md-list-item">
<p class="md-end-block">子组件 beforeMount</p>
</li>
<li class="md-list-item">
<p class="md-end-block">子组件 mounted |</p>
</li>
<li class="md-list-item">
<p class="md-end-block">父组件 mounted</p>
</li>
</ol>
<h5 class="md-end-block md-heading">2)更新过程</h5>
<ol class="ol-list" start="">
<li class="md-list-item">
<p class="md-end-block">父组件 beforeUpdate |</p>
</li>
<li class="md-list-item">
<p class="md-end-block">子组件 beforeUpdate</p>
</li>
<li class="md-list-item">
<p class="md-end-block">子组件 updated |</p>
</li>
<li class="md-list-item">
<p class="md-end-block">父组件 updated</p>
</li>
</ol>
<h5 class="md-end-block md-heading">3)销毁过程</h5>
<ol class="ol-list" start="">
<li class="md-list-item">
<p class="md-end-block">父组件 beforeDestroy</p>
</li>
<li class="md-list-item">
<p class="md-end-block">子组件 beforeDestroy |</p>
</li>
<li class="md-list-item">
<p class="md-end-block">子组件 destroyed</p>
</li>
<li class="md-list-item">
<p class="md-end-block"><span class="md-expand">父组件 destoryed |</span></p>
</li>
</ol>
<h3>(4)举个例子</h3>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Vue({
el: </span>'#app'<span style="color: rgba(0, 0, 0, 1)">,
data: {
    message: </span>''<span style="color: rgba(0, 0, 0, 1)">,
    userData: </span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">
},
beforeCreate() {
    console.log(</span>'beforeCreate hook'<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 发送初始化请求获取初始消息,并将其存储在message属性中。</span>
    axios.get('/api/initialize').then(response =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.message =<span style="color: rgba(0, 0, 0, 1)"> response.data.message;
    });
},
created() {
    console.log(</span>'created hook'<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 注册事件,注册了一个滚动事件的监听器</span>
    window.addEventListener('scroll', <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.handleScroll);
},
beforeMount() {
    console.log(</span>'beforeMount hook'<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 挂载之前请求数据,发送了一个请求来获取用户数据,并将其存储在userData属性中</span>
    axios.get('/api/userData').then(response =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.userData =<span style="color: rgba(0, 0, 0, 1)"> response.data;
    });
},
mounted() {
    console.log(</span>'mounted hook'<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 执行DOM操作,将特定元素的文字颜色设置为红色</span>
    <span style="color: rgba(0, 0, 255, 1)">this</span>.$refs.myElement.style.color = 'red'<span style="color: rgba(0, 0, 0, 1)">;
},
beforeUpdate() {
    console.log(</span>'beforeUpdate hook'<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 发送更新请求,将userData数据发送至后台</span>
    axios.post('/api/update', { data: <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.userData });
},
updated() {
    console.log(</span>'updated hook'<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 在更新后执行额外逻辑,例如调用了handleUpdatedLogic方法</span>
    <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.handleUpdatedLogic();
},
destroyed() {
    console.log(</span>'destroyed hook'<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 移除事件监听器,移除了之前注册的滚动事件的监听器</span>
    window.removeEventListener('scroll', <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.handleScroll);
},
methods: {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">包含了各种方法的对象。您可以在methods对象中定义组件中需要使用的方法;这些方法可以在模板中通过事件绑定或其他方式进行调用</span>
<span style="color: rgba(0, 0, 0, 1)">    handleScroll() {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 处理滚动事件</span>
      console.log('Scrolling...'<span style="color: rgba(0, 0, 0, 1)">);
    },
    handleUpdatedLogic() {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 更新后执行的额外逻辑</span>
      console.log('Additional logic after update'<span style="color: rgba(0, 0, 0, 1)">);
    }
}
});</span></span></pre>
</div>
<h2>三、vue3生命周期(组合式API)</h2>
<h3>(1)组合式API:</h3>
<p style="text-align: center">不是通过声明式方式进行声明,而是在<code>setup()</code>函数中调用相应的函数来使用</p>
<table border="0">
<tbody>
<tr>
<td style="text-align: center">setup()</td>
<td>始创建组件之前,在 beforeCreate 和 created 之前执行,创建的是 data 和 method</td>
</tr>
<tr>
<td>onBeforeMount()</td>
<td>组件挂载到节点上之前执行的函数</td>
</tr>
<tr>
<td>onMounted() :</td>
<td>组件挂载完成后执行的函数</td>
</tr>
<tr>
<td>onBeforeUpdate()</td>
<td>组件更新之前执行的函数</td>
</tr>
<tr>
<td>onUpdated():</td>
<td>组件更新完成之后执行的函数</td>
</tr>
<tr>
<td>onBeforeUnmount()</td>
<td>组件卸载之前执行的函数</td>
</tr>
<tr>
<td>onUnmounted():</td>
<td>组件卸载完成后执行的函数</td>
</tr>
<tr>
<td>onActivated()</td>
<td>被包含在 中的组件,会多出两个生命周期钩子函数,被激活时执行;</td>
</tr>
<tr>
<td>onDeactivated()</td>
<td>比如从 A 组件,切换到 B 组件,A 组件消失时执行;</td>
</tr>
<tr>
<td>onErrorCaptured()</td>
<td>当捕获一个来自子孙组件的异常时激活钩子函数。</td>
</tr>
</tbody>
</table>
<p>其他组合式API:</p>
<ul>
<li><code>watch()</code>用于监听响应式数据的变化</li>
<li><code>provide</code>和<code>inject</code>用于提供和注入依赖等。</li>
</ul>
<h3>(2)举个例子:</h3>
<div class="cnblogs_code">
<pre><span style="font-size: 14px">import { onBeforeMount, onMounted, onBeforeUpdate, onUpdated, onBeforeUnmount, onUnmounted, onActivated, onDeactivated, onErrorCaptured } from 'vue'<span style="color: rgba(0, 0, 0, 1)">;

export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
name: </span>'MyComponent'<span style="color: rgba(0, 0, 0, 1)">,
setup() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 组件挂载之前执行的函数</span>
    onBeforeMount(() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(</span>'Component is about to be mounted'<span style="color: rgba(0, 0, 0, 1)">);
    });

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 组件挂载完成后执行的函数</span>
    onMounted(() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(</span>'Component has been mounted'<span style="color: rgba(0, 0, 0, 1)">);
    });

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 组件更新之前执行的函数</span>
    onBeforeUpdate(() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(</span>'Component is about to update'<span style="color: rgba(0, 0, 0, 1)">);
    });

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 组件更新完成后执行的函数</span>
    onUpdated(() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(</span>'Component has been updated'<span style="color: rgba(0, 0, 0, 1)">);
    });

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 组件卸载之前执行的函数</span>
    onBeforeUnmount(() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(</span>'Component is about to be unmounted'<span style="color: rgba(0, 0, 0, 1)">);
    });

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 组件卸载完成后执行的函数</span>
    onUnmounted(() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(</span>'Component has been unmounted'<span style="color: rgba(0, 0, 0, 1)">);
    });

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 被激活时执行的函数,例如从父组件切换到子组件时</span>
    onActivated(() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(</span>'Component has been activated'<span style="color: rgba(0, 0, 0, 1)">);
    });

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 组件不再处于活动状态时执行的函数,例如从子组件切换到父组件时</span>
    onDeactivated(() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(</span>'Component has been deactivated'<span style="color: rgba(0, 0, 0, 1)">);
    });

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当捕获来自子组件的异常时激活的函数</span>
    onErrorCaptured((error, instance, info) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.error(</span>'Error captured:'<span style="color: rgba(0, 0, 0, 1)">, error);
    });

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义方法:打印一条消息</span>
    const printMessage = (message) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(message);
    };

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
      printMessage
    };
}
};<br></span></span></pre>
</div>
<h2>四、uni-app生命周期</h2>
<p><strong>1、应用程序的生命周期方法</strong>:定义在根组件app.vue中</p>
<p>onLaunch() 应用程序启动<br>onShow() 应用程序显示<br>onHide() 应用程序隐藏</p>
<div class="cnblogs_code">
<pre><span style="font-size: 14px">&lt;template&gt;
&lt;div&gt;
    &lt;h1&gt;Hello Vue!&lt;/h1&gt;
&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">
export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
name: </span>'App'<span style="color: rgba(0, 0, 0, 1)">,
created() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 应用程序启动时执行的函数</span>
    <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.onLaunch();
},
mounted() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 应用程序显示时执行的函数</span>
    <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.onShow();
},
beforeUnmount() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 应用程序隐藏时执行的函数</span>
    <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.onHide();
},
methods: {
    onLaunch() {
      console.log(</span>'Application launched'<span style="color: rgba(0, 0, 0, 1)">);
    },
    onShow() {
      console.log(</span>'Application shown'<span style="color: rgba(0, 0, 0, 1)">);
    },
    onHide() {
      console.log(</span>'Application hidden'<span style="color: rgba(0, 0, 0, 1)">);
    }
}
};
</span>&lt;/script&gt;</span></pre>
</div>
<p>&nbsp;</p>
<p><strong>2、页面的生命周期方法</strong>--仿微信小程序</p>
<p>onLoad() 页面挂载完成<br>onShow() 页面被显示<br>onReady() 页面可以交互<br>onHide() 页面被隐藏<br>onUnload() 页面被卸载</p>
<p>onPageScroll() 页面发生滚动<br>onReachBottom() 页面滚动触底<br>onPullDownRefresh() 页面下拉刷新</p>
<div class="cnblogs_code">
<pre><span style="font-size: 14px">&lt;template&gt;
&lt;div&gt;
    &lt;h1&gt;{{ message }}&lt;/h1&gt;
&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">
export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
name: </span>'MyPage'<span style="color: rgba(0, 0, 0, 1)">,

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 页面数据</span>
<span style="color: rgba(0, 0, 0, 1)">data() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
      message: </span>'Hello Vue!'<span style="color: rgba(0, 0, 0, 1)">,
    };
},

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 页面挂载完成时执行的函数</span>
<span style="color: rgba(0, 0, 0, 1)">mounted() {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.onLoad();
},

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 页面被显示时执行的函数</span>
<span style="color: rgba(0, 0, 0, 1)">activated() {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.onShow();
},

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 页面被隐藏时执行的函数</span>
<span style="color: rgba(0, 0, 0, 1)">deactivated() {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.onHide();
},

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 页面被卸载时执行的函数</span>
<span style="color: rgba(0, 0, 0, 1)">beforeUnmount() {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.onUnload();
},

methods: {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 页面挂载完成时执行的逻辑</span>
<span style="color: rgba(0, 0, 0, 1)">    onLoad() {
      console.log(</span>'Page loaded'<span style="color: rgba(0, 0, 0, 1)">);
    },

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 页面被显示时执行的逻辑</span>
<span style="color: rgba(0, 0, 0, 1)">    onShow() {
      console.log(</span>'Page shown'<span style="color: rgba(0, 0, 0, 1)">);
    },

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 页面被隐藏时执行的逻辑</span>
<span style="color: rgba(0, 0, 0, 1)">    onHide() {
      console.log(</span>'Page hidden'<span style="color: rgba(0, 0, 0, 1)">);
    },

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 页面被卸载时执行的逻辑</span>
<span style="color: rgba(0, 0, 0, 1)">    onUnload() {
      console.log(</span>'Page unloaded'<span style="color: rgba(0, 0, 0, 1)">);
    },

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 页面发生滚动时执行的逻辑</span>
<span style="color: rgba(0, 0, 0, 1)">    onPageScroll() {
      console.log(</span>'Page scrolled'<span style="color: rgba(0, 0, 0, 1)">);
    },

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 页面滚动触底时执行的逻辑</span>
<span style="color: rgba(0, 0, 0, 1)">    onReachBottom() {
      console.log(</span>'Page reached bottom'<span style="color: rgba(0, 0, 0, 1)">);
    },

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 页面下拉刷新时执行的逻辑</span>
<span style="color: rgba(0, 0, 0, 1)">    onPullDownRefresh() {
      console.log(</span>'Page pulled down for refresh'<span style="color: rgba(0, 0, 0, 1)">);
    },
},

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 监听页面滚动事件</span>
<span style="color: rgba(0, 0, 0, 1)">created() {
    window.addEventListener(</span>'scroll', <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.onPageScroll);
},

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 移除页面滚动事件监听</span>
<span style="color: rgba(0, 0, 0, 1)">beforeUnmount() {
    window.removeEventListener(</span>'scroll', <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.onPageScroll);
},
};
</span>&lt;/script&gt;</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/le-cheng/p/17624060.html
頁: [1]
查看完整版本: vue2生命周期、vue3生命周期、uni-app生命周期