Thuta Learning
Vue.js
BasicWeb Developmentအခြေခံ

Event Handling

Vue.jsသင်ခန်းစာ 820 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Inline နှင့် method event handlers သုံးရန်
  • prevent နှင့် key modifiers သုံးရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Event Handling ကို ရိုးရိုးလေးနားလည်မယ်

@click က v-on:click ၏ အတိုကောက်ဖြစ်သည်။ ရိုးရိုး assignment ကို inline handler ဖြင့်ရေးနိုင်ပြီး logic များလျှင် function သီးသန့်ရေးပါ။ .prevent, .stop နှင့် .enter ကဲ့သို့ modifier များက DOM logic ကိုရှင်းလင်းစေသည်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

submit event တွင် preventDefault မလုပ်ခြင်း

ဖြစ်လေ့ရှိတဲ့အမှား

Template ထဲတွင် logic အလွန်ရှည်စွာရေးခြင်း

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

v-on/@ နှင့် event modifiers များဖြင့် click, submit နှင့် keyboard event များကို ကိုင်တွယ်မည်။

Mental Model
  1. Props / event
  2. Component + template
  3. Rendered Vue UI

Inline နှင့် method event handlers သုံးရန်

အဓိက Syntax / Pattern

vue
<script setup>
import { ref } from 'vue'
const name = ref('')
function save() {
  console.log(`Saved: ${name.value}`)
}
</script>

<template>
  <form @submit.prevent="save">
    <input v-model="name" @keyup.esc="name = ''">
    <button>Save</button>
  </form>
</template>

v-on/@ နှင့် event modifiers များဖြင့် click, submit နှင့် keyboard event များကို ကိုင်တွယ်မည်။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Props / event<script setup>

    @click က v-on:click ၏ အတိုကောက်ဖြစ်သည်။ ရိုးရိုး assignment ကို inline handler ဖြင့်ရေးနိုင်ပြီး logic များလျှင် function သီးသန့်ရေးပါ။ .prevent, .stop နှင့် .enter ကဲ့သို့ modifier များက DOM logic ကိုရှင်းလင်းစေသည်။

  2. Component + template</script>

    v-on/@ နှင့် event modifiers များဖြင့် click, submit နှင့် keyboard event များကို ကိုင်တွယ်မည်။

  3. Rendered Vue UI</template>

    Event Handling ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Execution Flow
  1. Props / event
  2. Component + template
  3. Rendered Vue UI

Success

Inline နှင့် method event handlers သုံးရန်

Failure

submit event တွင် preventDefault မလုပ်ခြင်း

ရိုးရိုးလေး ရှင်းပြမယ်

@click က v-on:click ၏ အတိုကောက်ဖြစ်သည်။ ရိုးရိုး assignment ကို inline handler ဖြင့်ရေးနိုင်ပြီး logic များလျှင် function သီးသန့်ရေးပါ။ .prevent, .stop နှင့် .enter ကဲ့သို့ modifier များက DOM logic ကိုရှင်းလင်းစေသည်။

vue
<script setup>
import { ref } from 'vue'
const name = ref('')
function save() {
  console.log(`Saved: ${name.value}`)
}
</script>

<template>
  <form @submit.prevent="save">
    <input v-model="name" @keyup.esc="name = ''">
    <button>Save</button>
  </form>
</template>
You should see
Saved: Mya

လက်တွေ့စမ်းကြည့်ရန်

Enter နှိပ်လျှင် item အသစ်ထည့်ပြီး Escape နှိပ်လျှင် input ရှင်းသည့် form တစ်ခုရေးပါ။

Event Handling — Vue.js

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Event Handling ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Enter နှိပ်လျှင် item အသစ်ထည့်ပြီး Escape နှိပ်လျှင် input ရှင်းသည့် form တစ်ခုရေးပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
vue
<script setup>
import { ref } from 'vue'
const name = ref('')
function save() {
  console.log(`Saved: ${name.value}`)
}
</script>

<template>
  <form @submit.prevent="save">
    <input v-model="name" @keyup.esc="name = ''">
    <button>Save</button>
  </form>
</template>

ရလဒ်: Inline နှင့် method event handlers သုံးရန်

အရိပ်အမြွက် ကြည့်ရန်

Event Handling ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Quick Quiz 1 / 3

Event Handling ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • Props / event → Component + template → Rendered Vue UI
  • v-on/@ နှင့် event modifiers များဖြင့် click, submit နှင့် keyboard event များကို ကိုင်တွယ်မည်။
  • submit event တွင် preventDefault မလုပ်ခြင်း
  • Event Handling ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Event Handling — Vue.js

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • submit event တွင် preventDefault မလုပ်ခြင်း
  • Template ထဲတွင် logic အလွန်ရှည်စွာရေးခြင်း

လေ့ကျင့်ခန်း

Enter နှိပ်လျှင် item အသစ်ထည့်ပြီး Escape နှိပ်လျှင် input ရှင်းသည့် form တစ်ခုရေးပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Saved: Mya

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Event Handling | Thuta Learning