Asosiy matnga o'tish
ITIT Interview
Vue.js

Hayot sikli va template ref'lar

Komponent hayot sikli hooklari, onMounted, onUpdated, onUnmounted, Options API bilan mosligi, template ref'lar, useTemplateRef va xatolarni ushlash.

O'rganildi: 0 / 5
Progress

Hayot sikli

setup (script setup bajariladi)
  ↓
onBeforeMount  →  onMounted        DOM'ga qo'shildi
  ↓
reaktiv ma'lumot o'zgaradi
onBeforeUpdate →  onUpdated        DOM yangilandi
  ↓
komponent olib tashlanadi
onBeforeUnmount → onUnmounted      tozalash
<script setup lang="ts">
import { onMounted, onUnmounted, onUpdated, ref } from 'vue';

const width = ref(window.innerWidth);
const onResize = () => (width.value = window.innerWidth);

onMounted(() => {
  window.addEventListener('resize', onResize);   // DOM va brauzer API'lari mavjud
});

onUpdated(() => {
  // DOM yangilangandan keyin (kam ishlatiladi)
});

onUnmounted(() => {
  window.removeEventListener('resize', onResize);   // tozalash
});
</script>
HookQachonIshlatilishi
script setupKomponent yaratilgandaReaktiv state, ma'lumot so'rovini boshlash
onMountedDOM'ga qo'shilgandan keyinDOM bilan ishlash, tashqi kutubxonalar, tinglovchilar
onUpdatedHar DOM yangilanishidan keyinKamdan-kam; state o'zgartirmang (cheksiz sikl)
onUnmountedOlib tashlangandan keyinTinglovchi, taymer, obunalarni tozalash
onErrorCapturedBola komponentda xatoXato chegarasi
onActivated / onDeactivatedKeepAlive ichidaKeshlangan komponent ko'rinish/yashirinishi

Options API mosligi: created va beforeCreate o'rniga script setup kodi, mounted → onMounted, beforeDestroy/destroyed (Vue 2) → onBeforeUnmount/onUnmounted.

SSR'da faqat script setup kodi serverda ishlaydi: onMounted va undan keyingi hooklar faqat brauzerda chaqiriladi.

Template ref'lar

DOM elementi yoki bola komponentga to'g'ridan-to'g'ri murojaat:

<script setup lang="ts">
import { onMounted, useTemplateRef } from 'vue';

const input = useTemplateRef<HTMLInputElement>('search');   // Vue 3.5+

onMounted(() => {
  input.value?.focus();       // onMounted'gacha null
});
</script>

<template>
  <input ref="search" />
</template>

Eski usul: const search = ref<HTMLInputElement | null>(null) va shablonda ref="search" (nomlar mos bo'lishi kerak).

Bola komponent metodlari

script setup komponentlari standart holda yopiq: ota faqat defineExpose bilan ochilgan narsalarga murojaat qila oladi.

Modal.vue
<script setup>
import { ref } from 'vue';
const isOpen = ref(false);
const open = () => (isOpen.value = true);
const close = () => (isOpen.value = false);
defineExpose({ open, close });
</script>
<Modal ref="modal" />
<button @click="modal?.open()">Ochish</button>

Imkon qadar props va emits ishlating: defineExpose imperativ API kerak bo'lgan hollar uchun (fokus, scroll, modal).

Xatolarni ushlash

import { onErrorCaptured } from 'vue';

onErrorCaptured((err, instance, info) => {
  logError(err, info);
  return false;          // xato yuqoriga ko'tarilmasin
});

// Global
app.config.errorHandler = (err, instance, info) => reportToSentry(err);

Intervyu savollari

Ushbu sahifada