فهرست سرفصل‌های Vue
خانه (HOME) معرفی (Intro) دایرکتیوها (Directives) v-bind (v-bind) v-if (v-if) v-show (v-show) v-for (v-for) رویدادها (Events) v-on (v-on) متدها (Methods) تغییردهنده های رویداد (Event Modifiers) فرم ها (Forms) v-model (v-model) بایندینگ CSS (CSS Binding) ویژگی های محاسبه شده (Computed Properties) واچرها (Watchers) قالب ها (Templates) چرا، چگونه و راه اندازی (Why, How and Setup) اولین صفحه SFC (First SFC Page) کامپوننت ها (Components) پراپس (Props) کامپوننت های v-for (v-for Components) $emit() ($emit()) ویژگی های عبوری (Fallthrough) (Fallthrough Attributes) استایل Scoped (Scoped Styling) کامپوننت های محلی (Local Components) اسلات ها (Slots) v-slot (v-slot) اسلات های Scoped (Scoped Slots) کامپوننت های پویا (Dynamic Components) Teleport (Teleport) درخواست HTTP (HTTP Request) رفرنس های تمپلیت (Template Refs) هوک های چرخه عمر (Lifecycle Hooks) Provide/Inject (Provide/Inject) مسیریابی (Routing) ورودی های فرم (Form Inputs) انیمیشن ها (Animations) انیمیشن با v-for (Animations with v-for) بیلد (Build) Composition API (Composition API) ویژگی های توکار (Built-in Attributes) ویژگی 'is' ('is' Attribute) ویژگی 'key' ('key' Attribute) ویژگی 'ref' ('ref' Attribute) کامپوننت های توکار (Built-in Components) <KeepAlive> (<KeepAlive>) <Teleport> (<Teleport>) <Transition> (<Transition>) <TransitionGroup> (<TransitionGroup>) المان های توکار (Built-in Elements) <component> (<component>) <slot> (<slot>) <template> (<template>) نمونه کامپوننت (Component Instance) $attrs ($attrs) $data ($data) $el ($el) $parent ($parent) $props ($props) $refs ($refs) $root ($root) $slots ($slots) $emit() ($emit()) $forceUpdate() ($forceUpdate()) $nextTick() ($nextTick()) $watch() ($watch()) دایرکتیوها (Directives) v-bind (v-bind) v-cloak (v-cloak) v-for (v-for) v-html (v-html) v-if (v-if) v-else-if (v-else-if) v-else (v-else) v-memo (v-memo) v-model (v-model) v-on (v-on) v-once (v-once) v-pre (v-pre) v-show (v-show) v-slot (v-slot) v-text (v-text) گزینه های نمونه (Instance Options) داده ها (data) متدها (methods) محاسبه شده ها (computed) watch (watch) پراپس (props) emits (emits) expose (expose) هوک های چرخه عمر (Lifecycle Hooks) beforeCreate (beforeCreate) created (created) beforeMount (beforeMount) mounted (mounted) beforeUpdate (beforeUpdate) updated (updated) beforeUnmount (beforeUnmount) unmounted (unmounted) errorCaptured (errorCaptured) renderTracked (renderTracked) renderTriggered (renderTriggered) activated (activated) deactivated (deactivated) serverPrefetch (serverPrefetch) مثال ها (Examples) تمرین ها (Exercises) کوییز (Quiz) سیلابس (Syllabus) برنامه مطالعه (Study Plan) سرور (Server) گواهینامه (Certificate)
نتیجه‌ای برای جستجو یافت نشد.
Vue

Vue — v-on (v-on)

آخرین بروزرسانی: 1404/08/21

v-on (v-on)

اینجا «دستور v-on ویو» را خیلی ساده می گوییم. «رویداد (Event)» یعنی واکنش به کاری کاربر. مانند کلیک یا اسکرول. با v-on یک گوش دادن می سازیم. سپس هنگام رخداد، کد ما اجرا می شود.

تعریف و کاربرد اصلی

v-on روی تگ یا کامپوننت می آید. نوع رویداد را می گیرد. همچنین می تواند «مودیفایر (Modifier)» بگیرد. سپس یک متد یا عبارت اجرا می کند. میانبر @ برابر v-on: است.

مثال سریع: تغییر پس زمینه با کلیک

با کلیک، مقدار bgColor عوض می شود. سپس کلاس واکنش نشان می دهد.

<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>

مشاهده در ادیتور

مودیفایر capture: اول والد بگیرد

.capture یعنی رخداد ابتدا در والد گرفته شود. سپس فرزند.

<template>
  <h1>v-on Example</h1>
  <p>With '.capture', parent handles the click first.</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>
  <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>

مشاهده در ادیتور

مودیفایر stop: جلوی حباب سازی

.stop انتشار رویداد را متوقف می کند. والد دیگر نمی گیرد.

<template>
  <h1>v-on Example</h1>
  <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>
  <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>

مشاهده در ادیتور

passive و once برای کارایی

.passive می گوید پیش فرض لغو نمی شود. اسکرول روان تر می شود. .once فقط یک بار اجرا می کند.

<template>
  <h1>v-on Example</h1>
  <div v-on:scroll.passive="this.scrollTimes++" id="parent">
    <p>Scroll here.</p>
  </div>
  <p>Scroll happened {{ scrollTimes }} times.</p>
</template>
<script>
export default {
  data() {
    return {
      scrollTimes: 0
    };
  }
};
<\/script>
<style scoped>
#parent {
  width: 200px;
  height: 50px;
  overflow: scroll;
}
<\/style>

مشاهده در ادیتور

<template>
  <h1>v-on Example</h1>
  <button v-on:click.once="clickTimes++">Click</button>
  <p>Click event happened {{ clickTimes }} times.</p>
</template>
<script>
export default {
  data() {
    return {
      clickTimes: 0
    };
  }
};
<\/script>

مشاهده در ادیتور

self و prevent و کلیدها

.self فقط رویداد خود عنصر را می گیرد. .prevent پیش فرض را لغو می کند. همچنین می توان کلیدها را ترکیب کرد. مانند .left.shift.

<template>
  <h1>v-on Example</h1>
  <div v-on:click="addMsg('grandParent')" id="grandParent">
    Grandparent
    <div v-on:click.self="addMsg('parent')">
      Parent
      <div v-on:click="addMsg('child')">
        Child. CLICK HERE!
      </div>
    </div>
  </div>
  <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>
#grandParent {
  width: 250px;
}
<\/style>

مشاهده در ادیتور

<template>
  <h1>v-on Example</h1>
  <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 {
  width: 200px;
}
<\/style>

مشاهده در ادیتور

<template>
  <h1>v-on Example</h1>
  <p>Hold 'Shift' and left-click 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>

مشاهده در ادیتور

گام های عملی

  1. نوع رویداد را مشخص کن. مثلا click یا scroll.
  2. متد مناسب را بنویس. سپس در methods بگذار.
  3. در صورت نیاز، مودیفایر مناسب اضافه کن.
  4. برای کلیدها، از .left و .right و ترکیب ها کمک بگیر.

نکته: برای آموزش های مرتبط، سر بزن به v-model و v-once. همچنین لینک کلیدی: دستور v-on ویو.

جمع بندی سریع

  • v-on رویداد را می گیرد و کد را اجرا می کند.
  • میانبر @ برابر v-on: است.
  • .stop و .capture انتشار را کنترل می کنند.
  • .once یک بار اجرا می کند؛ .passive کارایی را بهتر می کند.
  • .self و کلیدها رفتار دقیق تری می دهند.