Vue 3中常用的生命周期鉤子和監(jiān)聽器的操作方法
前言
分析常用的一些生命周期鉤子和監(jiān)聽器可以幫助我們在組件中處理數(shù)據(jù)加載、狀態(tài)變化和響應式更新
1. onMounted
生命周期鉤子,在組件掛載后執(zhí)行。它適合用于初始化數(shù)據(jù)加載或執(zhí)行一次性的操作
<template>
<div>
<p>當前用戶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ù)據(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)聽指定的響應式數(shù)據(jù),并在數(shù)據(jù)變化時執(zhí)行回調函數(shù)
有兩種形式:基礎的 watch 和 watchEffect
<template>
<div>
<p>當前計數(shù): {{ count }}</p>
<button @click="increment">增加計數(shù)</button>
</div>
</template>
<script setup>
import { ref, watch } from 'vue';
const count = ref(0);
watch(count, (newValue, oldValue) => {
console.log(`計數(shù)從 ${oldValue} 變?yōu)?${newValue}`);
});
const increment = () => {
count.value++;
};
</script>watchEffect 在其依賴變化時立即執(zhí)行傳入的回調函數(shù)
<template>
<div>
<p>當前時間: {{ currentTime }}</p>
</div>
</template>
<script setup>
import { ref, watchEffect } from 'vue';
const currentTime = ref(new Date());
watchEffect(() => {
console.log('更新當前時間');
currentTime.value = new Date();
});
// 模擬定時更新時間
setInterval(() => {
currentTime.value = new Date();
}, 1000);
</script>3. computed
computed: 計算屬性,基于響應式數(shù)據(jù)派生出新的數(shù)據(jù),并自動緩存結果
<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)建一個完全響應式的對象或數(shù)組
<template>
<div>
<p>當前用戶: {{ 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)建一個響應式的引用,通常用于包裝基本類型值
<template>
<div>
<p>當前計數(shù): {{ count.value }}</p>
<button @click="increment">增加計數(shù)</button>
</div>
</template>
<script setup>
import { ref } from 'vue';
const count = ref(0);
const increment = () => {
count.value++;
};
</script>到此這篇關于Vue 3中常用的生命周期鉤子和監(jiān)聽器的詳細分析的文章就介紹到這了,更多相關Vue 3生命周期鉤子和監(jiān)聽器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue.js?Table?組件自定義列寬實現(xiàn)核心方法
這篇文章主要介紹了vue.js?Table?組件自定義列寬實現(xiàn)核心方法,文圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07
vue框架和react框架的區(qū)別以及各自的應用場景使用
這篇文章主要介紹了vue框架和react框架的區(qū)別以及各自的應用場景使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
淺談Vue網(wǎng)絡請求之interceptors實際應用
這篇文章主要介紹了淺談Vue網(wǎng)絡請求之interceptors實際應用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
vue jsx 使用指南及vue.js 使用jsx語法的方法
這篇文章主要介紹了vue jsx 使用指南及vue.js 使用jsx語法的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11

