$el
Misol
Root darajadagi <div> tegining fon rangini o‘zgartirish uchun $el obyektidan foydalanish.
methods: {
changeColor() {
this.$el.style.backgroundColor = 'lightgreen';
}
}
Misolni ishga tushirish »
Quyida ko‘proq misollarni ko‘ring.
Ta’rif va qo‘llanilishi
$el obyekti Vue komponentining root DOM tugunini ifodalaydi.
Vue ilovasi mount qilinmaguncha (DOMga o‘rnatilmaguncha) $el obyekti mavjud bo‘lmaydi.
<template> ichida faqat bitta HTML root element bo‘lsa:
- $el obyekti o‘sha root elementi bo‘ladi.
- DOM to‘g‘ridan-to‘g‘ri $el obyekti yordamida manipulyatsiya qilinishi mumkin (yuqoridagi misolga qarang), lekin tavsiya etilmaydi.
- DOMni deklarativ tarzda o‘zgartirish uchun Vue ref atributidan va Vue’ning boshqa imkoniyatlaridan foydalangan ma’qul, chunki bu kodni izchilroq va qo‘llab-quvvatlashni osonroq qiladi (quyidagi 1-misolga qarang).
<template> ichida birdan ortiq HTML root element bo‘lsa:
- $el obyekti Vue ichki maqsadlarda ishlatadigan joy egasi (placeholder) DOM matn tuguni bo‘ladi, xolos (haqiqiy DOM elementi emas).
- Agar bir nechta root elementlari mavjud bo‘lsa, DOMni $el obyekti yordamida manipulyatsiya qilib bo‘lmaydi (quyida 2-misolga qarang).
Eslatma: Vue 3 Composition API’sida <script setup> ichida (setup funksiyasidan foydalanilganda) $el xususiyatiga murojaat qilib bo‘lmaydi.
Ko‘proq misollar
1-misol
$el obyekti o‘rniga, tavsiya etilganidek, <div> tegining fon rangini deklarativ tarzda o‘zgartirish uchun ref atributidan foydalanish.
<template>
<div ref="rootDiv">
<h2>Example $el Object</h2>
<p>It is recommended, and more consistent, to use the ref attribute instead of the $el object to change the background color root DIV tag.</p>
<button v-on:click="changeColor">Click here</button>
</div>
</template>
<script>
export default {
methods: {
changeColor() {
this.$refs.rootDiv.style.backgroundColor = 'lightgreen';
}
}
}
</script>
Misolni ishga tushirish »
2-misol
<template>’ning root darajasida birdan ortiq element bo‘lsa, $el obyekti root elementlarning birinchisini ifodalovchi, Vue ichki maqsadlarda ishlatadigan matn tuguni (haqiqiy DOM elementi emas) bo‘ladi, xolos.
Bunday hollarda biz DOMni $el obyekti bilan boshqara olmaymiz.
<template>
<div>
<h2>Example $el Object</h2>
<p>We are not able to use the $el object to change the background color of the root DIV tag when there are other tags on the root level. Open browser console to see the error generated.</p>
<button v-on:click="changeColor">Click here</button>
</div>
<p>With this extra p-tag there are two tags on the root level.</p>
</template>
<script>
export default {
methods: {
changeColor() {
this.$el.style.backgroundColor = 'lightgreen';
}
}
}
</script>
<style>
#app > div, #app > p {
border: solid black 1px;
padding: 10px;
}
</style>
Misolni ishga tushirish »
3-misol
<h2> bola elementining fon rangini o‘zgartirish uchun $el obyektidan foydalanish.
<template>
<div>
<h2>Example $el Object</h2>
<p>Using the $el object to change the background color of the H2 child element.</p>
<button v-on:click="changeColor">Click here</button>
</div>
</template>
<script>
export default {
methods: {
changeColor() {
this.$el.children[0].style.backgroundColor = 'lightblue';
}
}
}
</script>
Misolni ishga tushirish »
Tegishli sahifalar
Vue darsligi: Vue shablon reflari (template refs)
Vue darsligi: Vue metodlari
Vue ma’lumotnomasi: Vue ref atributi
Vue ma’lumotnomasi: Vue $refs obyekti
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
