Vue 3中常用的生命周期鉤子和監(jiān)聽器的操作方法
前言
分析常用的一些生命周期鉤子和監(jiān)聽器可以幫助我們在組件中處理數(shù)據(jù)加載、狀態(tài)變化和響應(yīng)式更新
1. onMounted
生命周期鉤子,在組件掛載后執(zhí)行。它適合用于初始化數(shù)據(jù)加載或執(zhí)行一次性的操作
<template>
<div>
<p>當(dāng)前用戶ID: {{ userId }}</p>
<button @click="fetchUserData">加載用戶數(shù)據(jù)</button>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import UserDataApi from 'path-to-your-api'; // 假設(shè)這是你的數(shù)據(jù)接口
const userId = ref(null);
const userData = ref(null);
onMounted(async () => {
// 初始化加載數(shù)據(jù)
await fetchUserData();
});
const fetchUserData = async () => {
const response = await UserDataApi.getUserData();
userId.value = response.userId;
userData.value = response.userData;
};
</script>2. watch
監(jiān)聽指定的響應(yīng)式數(shù)據(jù),并在數(shù)據(jù)變化時執(zhí)行回調(diào)函數(shù)
有兩種形式:基礎(chǔ)的 watch 和 watchEffect
<template>
<div>
<p>當(dāng)前計(jì)數(shù): {{ count }}</p>
<button @click="increment">增加計(jì)數(shù)</button>
</div>
</template>
<script setup>
import { ref, watch } from 'vue';
const count = ref(0);
watch(count, (newValue, oldValue) => {
console.log(`計(jì)數(shù)從 ${oldValue} 變?yōu)?${newValue}`);
});
const increment = () => {
count.value++;
};
</script>watchEffect 在其依賴變化時立即執(zhí)行傳入的回調(diào)函數(shù)
<template>
<div>
<p>當(dāng)前時間: {{ currentTime }}</p>
</div>
</template>
<script setup>
import { ref, watchEffect } from 'vue';
const currentTime = ref(new Date());
watchEffect(() => {
console.log('更新當(dāng)前時間');
currentTime.value = new Date();
});
// 模擬定時更新時間
setInterval(() => {
currentTime.value = new Date();
}, 1000);
</script>3. computed
computed: 計(jì)算屬性,基于響應(yīng)式數(shù)據(jù)派生出新的數(shù)據(jù),并自動緩存結(jié)果
<template>
<div>
<p>原始數(shù)值: {{ originalValue }}</p>
<p>加倍后的值: {{ doubledValue }}</p>
<button @click="increment">增加數(shù)值</button>
</div>
</template>
<script setup>
import { ref, computed } from 'vue';
const originalValue = ref(5);
const doubledValue = computed(() => originalValue.value * 2);
const increment = () => {
originalValue.value++;
};
</script>4. 其他
reactive: 創(chuàng)建一個完全響應(yīng)式的對象或數(shù)組
<template>
<div>
<p>當(dāng)前用戶: {{ user.name }}</p>
<button @click="changeUserName">修改用戶名</button>
</div>
</template>
<script setup>
import { reactive } from 'vue';
const user = reactive({
name: 'John Doe',
age: 30
});
const changeUserName = () => {
user.name = 'Jane Smith';
};
</script>ref: 創(chuàng)建一個響應(yīng)式的引用,通常用于包裝基本類型值
<template>
<div>
<p>當(dāng)前計(jì)數(shù): {{ count.value }}</p>
<button @click="increment">增加計(jì)數(shù)</button>
</div>
</template>
<script setup>
import { ref } from 'vue';
const count = ref(0);
const increment = () => {
count.value++;
};
</script>到此這篇關(guān)于Vue 3中常用的生命周期鉤子和監(jiān)聽器的詳細(xì)分析的文章就介紹到這了,更多相關(guān)Vue 3生命周期鉤子和監(jiān)聽器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
從0搭建Vue3組件庫如何使用?glup?打包組件庫并實(shí)現(xiàn)按需加載
這篇文章主要介紹了從0搭建Vue3組件庫如何使用?glup?打包組件庫并實(shí)現(xiàn)按需加載,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
el-table實(shí)現(xiàn)搜索高亮展示并滾動到元素位置的操作代碼
這篇文章主要介紹了el-table實(shí)現(xiàn)搜索高亮展示并滾動到元素位置,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01
vue2使用wangeditor實(shí)現(xiàn)數(shù)學(xué)公式和富文本編輯器
這篇文章主要為大家詳細(xì)介紹了vue2如何使用wangeditor實(shí)現(xiàn)數(shù)學(xué)公式和富文本編輯器功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-12-12
vue3如何解決各場景l(fā)oading過度(避免白屏尷尬!)
在開發(fā)的過程中點(diǎn)擊提交按鈕,或者是一些其它場景總會遇到loading加載,下面這篇文章主要給大家介紹了關(guān)于vue3如何解決各場景l(fā)oading過度的相關(guān)資料,避免白屏尷尬,需要的朋友可以參考下2023-03-03
手把手教你如何將html模板資源轉(zhuǎn)為vuecli項(xiàng)目
Vue可以直接集成html,Vue就是前端框架,使用Vue做前端開發(fā)效率非常高,下面這篇文章主要給大家介紹了關(guān)于如何將html模板資源轉(zhuǎn)為vuecli項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2023-04-04
vue項(xiàng)目關(guān)閉eslint校驗(yàn)
eslint是一個JavaScript的校驗(yàn)插件,通常用來校驗(yàn)語法或代碼的書寫風(fēng)格。這篇文章主要介紹了vue項(xiàng)目關(guān)閉eslint校驗(yàn),需要的朋友可以參考下2018-03-03
VUE+ElementUI下載文件的幾種方式(小結(jié))
本文主要介紹了VUE+ElementUI下載文件的幾種方式(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

