v-on

Vue v-on direktivasi


Misol

Tugma bosilganda <div> elementining fon rangini o‘zgartirish uchun v-on direktivasidan foydalanish.

<template>
  <h1>v-on Example</h1>
  <div v-bind:class="{ yelClass: bgColor }">
    <p>Click the button to change background color of this DIV box.</p>
    <button v-on:click="bgColor = !bgColor">Click</button>
    <p>bgColor property: "{{ bgColor }}"</p>
  </div>
</template>
Misolni ishga tushirish »

Quyida ko‘proq misollarni ko‘ring.


ULASHISH

Ta’rif va qo‘llanilishi

v-on direktivasi hodisa tinglovchisini (event listener) biriktirish uchun elementga joylashtiriladi.

v-on yordamida hodisa tinglovchisini biriktirish uchun hodisa turini, har qanday modifikatorni va shu hodisa sodir bo‘lganda ishga tushishi kerak bo‘lgan metod yoki ifodani ko‘rsatishimiz kerak.

Agar v-on oddiy HTML tegiga joylashtirilsa, tinglash uchun belgilashimiz mumkin bo‘lgan hodisa turlari input, click yoki mouseover kabi odatiy hodisalardir.

Agar v-on maxsus komponentga joylashtirilsa, tinglash uchun belgilashimiz mumkin bo‘lgan hodisa turlari shu komponentdan emit qilinadigan maxsus hodisalardir.

v-on: direktivasining qisqa shakli — @.


Modifikatorlar

Modifikator Tafsilotlar
.capture Ko‘tariluvchi (bubbling) hodisa birinchi navbatda .capture modifikatori o‘rnatilgan joyda ushlanadi. Misolni ishga tushirish »
.once Hodisa sahifa yuklangandan keyin faqat bir marta ishga tushishi mumkin. Misolni ishga tushirish »
.passive DOM elementida passive: true ni o‘rnatish orqali hodisa ishlovchisini (event handler) passiv deb belgilaydi. Bu shuni anglatadiki, brauzer event.preventDefault() chaqirilish-chaqirilmasligini kutib turishi shart emas va scroll kabi juda tez-tez sodir bo‘ladigan hodisalar uchun hodisa ishlovchisini passiv qilib belgilash unumdorlikni oshirishi mumkin. Misolni ishga tushirish »
.prevent Hodisaning ishga tushishiga yo‘l qo‘yilmaydi. Masalan, formaning standart submit hodisasining oldini olish uchun ishlatilishi mumkin. Barcha hodisalarning oldini olib bo‘lmaydi. Misolni ishga tushirish »
.stop Ko‘tariluvchi (bubbling) hodisaning keyingi tarqalishi to‘xtatiladi. Bunda event.stopPropagation() chaqiriladi. Misolni ishga tushirish »
.self Standart holatda hodisalar yuqoriga, ota elementlar tomon tarqaladi, shuning uchun bitta hodisa ko‘plab hodisa tinglovchilarini ishga tushirishi mumkin. .self modifikatori hodisa tinglovchisini faqat o‘z elementidan kelgan hodisalar ishga tushirishiga imkon beradi. Misolni ishga tushirish »
.{keyAlias} Hodisa ishlovchisini faqat ma’lum tugmalarga (event keys) javob beradigan qilib cheklaydi, masalan, v-on:click.right yoki v-on:keyup.s. Hatto ishlovchi javob berishi uchun bir vaqtning o‘zida bir nechta tugma bosilishini talab qilishimiz ham mumkin, masalan: v-on:click.left.shift. Misolni ishga tushirish »
.left Sichqonchaning chap tugmasi bilan bosish.
.right Sichqonchaning o‘ng tugmasi bilan bosish.
.middle Sichqonchaning o‘rta tugmasi bilan bosish.


Ko‘proq misollar

1-misol

click hodisasini birinchi navbatda ota elementda ushlash uchun .capture modifikatoridan foydalanish.

<template>
  <h1>v-on Example</h1>
  <p>When the '.capture' modifier is used on the parent DIV element, the event is captured first in the parent element when the child element is clicked.</p>
  <p>If the '.capture' modifier is removed from this code, the child element will capture the click event first. This is the default behavior, that the event bubbles up, first in child element, then to the parent.</p>
  <div v-on:click.capture="this.msg.push('parent')" id="parent">
    <p>This is the parent element.</p>
    <div v-on:click="this.msg.push('child')">
      <p>This is the child element. CLICK HERE!</p>
    </div>
  </div>
  <p>The order of when and where the event is captured.</p>
  <ol>
    <li v-for="x in msg">{{ x }}</li>
  </ol>
</template>

<script>
export default {
  data() {
    return {
      msg: []
    };
  }
}
</script>

<style scoped>
div {
  margin: 10px;
  padding: 10px;
  border: dashed black 1px;
}
#parent {
  width: 250px;
  background-color: lightpink;
}
#parent > div {
  cursor: pointer;
  background-color: lightgreen;
}
</style>
Misolni ishga tushirish »

2-misol

Hodisaning keyingi tarqalishining oldini olish uchun .stop modifikatoridan foydalanish.

<template>
  <h1>v-on Example</h1>
  <p>The '.stop' modifier stops the click event from propagating any further.</p>
  <p>If the '.stop' modifier is removed from this code, the parent element will also capture the click event on the child element.</p>
  <div v-on:click="this.msg.push('parent')" id="parent">
    <p>This is the parent element.</p>
    <div v-on:click.stop="this.msg.push('child')">
      <p>This is the child element. CLICK HERE!</p>
    </div>
  </div>
  <p>The order of when and where the event is captured.</p>
  <ol>
    <li v-for="x in msg">{{ x }}</li>
  </ol>
</template>

<script>
export default {
  data() {
    return {
      msg: []
    };
  }
}
</script>

<style scoped>
div {
  margin: 10px;
  padding: 10px;
  border: dashed black 1px;
}
#parent {
  width: 250px;
  background-color: lightpink;
}
#parent > div {
  cursor: pointer;
  background-color: lightgreen;
}
</style>
Misolni ishga tushirish »

3-misol

Sahifani aylantirish (scroll) paytida unumdorlikni oshirish uchun .passive modifikatoridan foydalanish.

<template>
  <h1>v-on Example</h1>
  <p>The '.passive' modifier sets the event handler as passive, and this can enhance performance.</p>
  <div v-on:scroll.passive="this.scrollTimes++" id="parent">
    <p>Scroll here.</p>
    <p>Bladi-bladi-bladi</p>
    <p>potato potato</p>
    <p>Scroll-scroll-scroll</p>
    <p>Scroll more...</p>
  </div>
  <p>Scroll happended {{ scrollTimes }} times.</p>
</template>

<script>
export default {
  data() {
    return {
      scrollTimes: 0
    };
  }
}
</script>

<style scoped>
div {
  margin: 10px;
  padding: 10px;
  border: dashed black 1px;
  width: 200px;
  height: 50px;
  overflow: scroll;
  background-color: lightcoral;
}
</style>
Misolni ishga tushirish »

4-misol

Sahifani aylantirish (scroll) paytida unumdorlikni oshirish uchun .once modifikatoridan foydalanish.

<template>
  <h1>v-on Example</h1>
  <p>The '.once' modifier prevents the event from happening more than once.</p>
  <button v-on:click.once="clickTimes++">Click</button>
  <p>Click event happened {{ clickTimes }} times.</p>
</template>

<script>
export default {
  data() {
    return {
      clickTimes: 0
    };
  }
}
</script>
Misolni ishga tushirish »

5-misol

Ota element faqat o‘zida sodir bo‘lgan click hodisalariga javob berishi uchun .self modifikatoridan foydalanish.

<template>
  <h1>v-on Example</h1>
  <p>The '.self' modifier is set on the parent element. </p>
  <p>Click on the child element and see how the event propagates past the parent element because the parent click event only reacts to click on the element itself.</p>
  <div v-on:click="addMsg('grandParent')" id="grandParent">
    Grandparent element
    <div v-on:click.self="addMsg('parent')">
      Parent element.
      <div v-on:click="addMsg('child')">
        Child element. CLICK HERE!
      </div>
    </div>
  </div>
  <p>The order of when and where the event is captured.</p>
  <ol>
    <li v-for="x in msg">{{ x }}</li>
  </ol>
</template>

<script>
export default {
  data() {
    return {
      msg: []
    };
  },
  methods: {
    addMsg(txt) {
      this.msg.push(txt);
    }
  }
}
</script>

<style scoped>
div {
  margin: 10px;
  padding: 10px;
  border: dashed black 1px;
  cursor: pointer;
}
#grandParent {
  width: 250px;
  background-color: lightpink;
}
#grandParent > div {
  background-color: lightgreen;
}
#grandParent > div > div {
  background-color: lightskyblue;
}
</style>
Misolni ishga tushirish »

6-misol

Sichqonchaning o‘ng tugmasi bosilganda standart ochiladigan ro‘yxat paydo bo‘lishining oldini olish uchun .prevent modifikatoridan foydalanish.

<template>
  <h1>v-on Example</h1>
  <p>The '.prevent' modifier is set to prevent the drop down menu to appear when the user does a right mouse button click.</p>
  <div 
    v-on:click.right.prevent="changeColor" 
    v-bind:style="{ backgroundColor: 'hsl(' + bgColor + ',80%,80%)' }">
    <p>Press right mouse button here.</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      bgColor: 0
    }
  },
  methods: {
    changeColor() {
      this.bgColor += 50
    }
  }
}
</script>

<style scoped>
div {
  margin: 10px;
  padding: 10px;
  border: dashed black 1px;
  width: 200px;
}
</style>
Misolni ishga tushirish »

7-misol

Foydalanuvchi shift tugmasini bosib turgan holda sichqonchaning chap tugmasini bosganda rasmni o‘zgartirish uchun .left.shift modifikatorlaridan foydalanish.

<template>
  <h1>v-on Example</h1>
  <p>Hold 'Shift' key and press left mouse button on the image:</p>
  <img v-on:click.left.shift="changeImg" v-bind:src="imgUrl">
</template>

<script>
export default {
  data() {
    return {
      imgFish: true,
      imgUrl: 'img_fish.svg'
    }
  },
  methods: {
    changeImg() {
      this.imgFish = !this.imgFish;
      if (this.imgFish) {
        this.imgUrl = 'img_fish.svg'
      }
      else {
        this.imgUrl = 'img_tiger.svg'
      }
    }
  }
}
</script>

<style scoped>
img {
  width: 200px;
}
</style>
Misolni ishga tushirish »

Tegishli sahifalar

Vue darsligi: Vue hodisalari

Vue darsligi: Vue v-on direktivasi

Vue darsligi: Vue metodlari

Vue darsligi: Vue hodisa modifikatorlari

JavaScript qo‘llanmasi: JavaScript hodisalari


W3Schools Pathfinder

Yutuqlaringizni kuzating – bu bepul!