ရိုးရိုးလေး ရှင်းပြမယ်
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 များကို ကိုင်တွယ်မည်။
- Props / event
- Component + template
- Rendered Vue UI
Inline နှင့် method event handlers သုံးရန်
အဓိက Syntax / Pattern
<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 ကို အဆင့်လိုက်ဖတ်မယ်
- Props / event
<script setup>@click က v-on:click ၏ အတိုကောက်ဖြစ်သည်။ ရိုးရိုး assignment ကို inline handler ဖြင့်ရေးနိုင်ပြီး logic များလျှင် function သီးသန့်ရေးပါ။ .prevent, .stop နှင့် .enter ကဲ့သို့ modifier များက DOM logic ကိုရှင်းလင်းစေသည်။
- Component + template
</script>v-on/@ နှင့် event modifiers များဖြင့် click, submit နှင့် keyboard event များကို ကိုင်တွယ်မည်။
- Rendered Vue UI
</template>Event Handling ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။
- Props / event
- Component + template
- 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 ကိုရှင်းလင်းစေသည်။
<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>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 ကိုခန့်မှန်းပါ
<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
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- 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