Vue.Js da Emit tushunchasi

Assalamu alaykum ! Vue.js da emit hodisalari yordamida komponentlar o’rtasida ma’lumot almashish imkoniyati mavjud. Emit hodisalari yordamida bola komponent ota komponentga ma’lumot yuborishi yoki xabar berishi mumkin. Quyida emit hodisalari haqida b…


This content originally appeared on DEV Community and was authored by Mukhriddin Khodiev (work)

  • Assalamu alaykum ! Vue.js da emit hodisalari yordamida komponentlar o'rtasida ma'lumot almashish imkoniyati mavjud. Emit hodisalari yordamida bola komponent ota komponentga ma'lumot yuborishi yoki xabar berishi mumkin. Quyida emit hodisalari haqida batafsil ko'rib chiqamiz.

emit Hodisalarini Aniqlash (event handler)

emit hodisalarini aniqlash va ishlatish uchun defineEmits funksiyasidan foydalaniladi.

Image description

Gooo:

  1. *Eventlarni aniqlash (event handler) *:
    defineEmits funksiyasidan foydalanib, komponentda chiqariladigan eventlarni ahndel qilsihimiz mumkin.

  2. Eventlarni chiqarish:
    Eventlarni kerakli joyda emit funksiyasi yordamida chiqarish mumkin.

Quyida batafsil msiloda ko'ramiz:

Misol 1: Oddiy Event

  1. Bola Komponent: ChildComponent.vue
<template>
  <button @click="notifyParent">Click me</button>
</template>

<script setup>
import { defineEmits } from 'vue'

// Eventlarni aniqlash
const emit = defineEmits(['myEvent'])

const notifyParent = () => {
  emit('myEvent', 'Assalamu alaykuuuum bratanim')
}
</script>
  1. Ota Komponent: ParentComponent.vue
<template>
  <div>
    <ChildComponent @myEvent="handleMyEvent" />
  </div>
</template>

<script setup>
import ChildComponent from './ChildComponent.vue'

const handleMyEvent = (message) => {
  console.log(message) // Output: Assalamu alaykuuuum bratanim
}
</script>

Misol 2: Argumentlar bilan Event

  1. Bola Komponent: ChildComponent.vue
<template>
  <button @click="sendData">Send Data</button>
</template>

<script setup>
const emit = defineEmits(['sendData'])

const sendData = () => {
  emit('sendData', { id: 1, name: 'Jonibek Davronov' })
}
</script>
  1. Ota Komponent: ParentComponent.vue
<template>
  <div>
    <ChildComponent @sendData="receiveData" />
  </div>
</template>

<script setup>
import ChildComponent from './ChildComponent.vue'

const receiveData = (data) => {
  console.log(data) // Output: { id: 1, name: 'Jonibek Davronov' }
}
</script>

Misol 3: Validatsiya bilan Event

  1. Bola Komponent: ChildComponent.vue
<template>
  <button @click="sendData">Send Validated Data</button>
</template>

<script setup>
const emit = defineEmits({
  // Event nomi va uni qabul qiladigan argumentlar uchun validatsiya
  sendData: (payload) => {
    if (typeof payload.id === 'number' && typeof payload.name === 'string') {
      return true
    } else {
      console.warn('Invalid payload')
      return false
    }
  }
})

const sendData = () => {
  emit('sendData', { { id: 1, name: 'Jonibek Davronov' })
}
</script>
  1. Ota Komponent: ParentComponent.vue
<template>
  <div>
    <ChildComponent @sendData="handleValidatedData" />
  </div>
</template>

<script setup>
import ChildComponent from './ChildComponent.vue'

const handleValidatedData = (data) => {
  console.log(data) // Output: { id: 1, name: 'Jonibek Davronov' }
}
</script>

Xulosa o'laroq:

Vue.js da emit hodisalari yordamida komponentlar o'rtasida ma'lumot almashish mumkin ekan. defineEmits funksiyasidan foydalanib eventlarni aniqlash va emit funksiyasi yordamida eventlarni chiqarish mumkin (ota componentaga) . Bu hodisalar yordamida bola komponent ota komponentga ma'lumot yuborishi yoki xabar berishi mumkin. Eventlarni validatsiya qilish orqali, yuborilgan ma'lumotlarni to'g'ri ekanligiga ishonch hosil qilishimiz mumkin ekan.

Bizni tarmoqlarda kuzatishingiz mumkin va maqola foydali bo'lsa izoh va Vuechi do'stlaringizga ulashing. 🫡

đź”— https://t.me/mukhriddinweb
đź”— https://medium.com/@mukhriddinweb
đź”— https://dev.to/mukhriddinweb
đź”— https://khodieff.uz
đź”— https://github.com/mukhriddin-dev
đź”— https://linkedin.com/in/mukhriddin-khodiev
đź”— https://youtube.com/@mukhriddinweb


This content originally appeared on DEV Community and was authored by Mukhriddin Khodiev (work)


Print Share Comment Cite Upload Translate Updates
APA

Mukhriddin Khodiev (work) | Sciencx (2024-07-22T21:58:28+00:00) Vue.Js da Emit tushunchasi. Retrieved from https://www.scien.cx/2024/07/22/vue-js-da-emit-tushunchasi/

MLA
" » Vue.Js da Emit tushunchasi." Mukhriddin Khodiev (work) | Sciencx - Monday July 22, 2024, https://www.scien.cx/2024/07/22/vue-js-da-emit-tushunchasi/
HARVARD
Mukhriddin Khodiev (work) | Sciencx Monday July 22, 2024 » Vue.Js da Emit tushunchasi., viewed ,<https://www.scien.cx/2024/07/22/vue-js-da-emit-tushunchasi/>
VANCOUVER
Mukhriddin Khodiev (work) | Sciencx - » Vue.Js da Emit tushunchasi. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/22/vue-js-da-emit-tushunchasi/
CHICAGO
" » Vue.Js da Emit tushunchasi." Mukhriddin Khodiev (work) | Sciencx - Accessed . https://www.scien.cx/2024/07/22/vue-js-da-emit-tushunchasi/
IEEE
" » Vue.Js da Emit tushunchasi." Mukhriddin Khodiev (work) | Sciencx [Online]. Available: https://www.scien.cx/2024/07/22/vue-js-da-emit-tushunchasi/. [Accessed: ]
rf:citation
» Vue.Js da Emit tushunchasi | Mukhriddin Khodiev (work) | Sciencx | https://www.scien.cx/2024/07/22/vue-js-da-emit-tushunchasi/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.