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

Custom Directives and Plugins

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

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

  • Custom directive lifecycle ကိုသုံးရန်
  • Directive နှင့် component ကိုရွေးချယ်ရန်
  • Plugin register လုပ်ရန်

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

Custom Directives and Plugins ကို ရိုးရိုးလေးနားလည်မယ်

Custom directive ကို low-level DOM behavior ပြန်လည်အသုံးပြုလိုသောအခါသာသုံးပါ။ UI structure ပါလျှင် component သို့မဟုတ် composable ပိုသင့်တော်သည်။ App-wide services နှင့် directives စုစည်းရန် plugin ၏ install(app) ကိုသုံးနိုင်သည်။

Problem → Why → Solution

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

ရိုးရိုး template logic ကို directive အဖြစ်ရှုပ်ထွေးစေခြင်း

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

Directive event listener ကို unmounted hook တွင်မရှင်းခြင်း

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

DOM behavior အတွက် custom directive နှင့် app-wide capability အတွက် plugin တည်ဆောက်နည်းကို လေ့လာမည်။

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

Custom directive lifecycle ကိုသုံးရန်

အဓိက Syntax / Pattern

vue
<script setup>
const vFocus = {
  mounted(element) {
    element.focus()
  }
}
</script>

<template>
  <label>
    Search
    <input v-focus placeholder="Type to search">
  </label>
</template>

DOM behavior အတွက် custom directive နှင့် app-wide capability အတွက် plugin တည်ဆောက်နည်းကို လေ့လာမည်။

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

  1. Props / event<script setup>

    Custom directive ကို low-level DOM behavior ပြန်လည်အသုံးပြုလိုသောအခါသာသုံးပါ။ UI structure ပါလျှင် component သို့မဟုတ် composable ပိုသင့်တော်သည်။ App-wide services နှင့် directives စုစည်းရန် plugin ၏ install(app) ကိုသုံးနိုင်သည်။

  2. Component + template</script>

    DOM behavior အတွက် custom directive နှင့် app-wide capability အတွက် plugin တည်ဆောက်နည်းကို လေ့လာမည်။

  3. Rendered Vue UI</template>

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

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

Success

Custom directive lifecycle ကိုသုံးရန်

Failure

ရိုးရိုး template logic ကို directive အဖြစ်ရှုပ်ထွေးစေခြင်း

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

Custom directive ကို low-level DOM behavior ပြန်လည်အသုံးပြုလိုသောအခါသာသုံးပါ။ UI structure ပါလျှင် component သို့မဟုတ် composable ပိုသင့်တော်သည်။ App-wide services နှင့် directives စုစည်းရန် plugin ၏ install(app) ကိုသုံးနိုင်သည်။

vue
<script setup>
const vFocus = {
  mounted(element) {
    element.focus()
  }
}
</script>

<template>
  <label>
    Search
    <input v-focus placeholder="Type to search">
  </label>
</template>
You should see
Search input receives focus after mount

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

Element အပြင်ဘက် click လျှင် callback ခေါ်သော v-click-outside custom directive တစ်ခုရေးပါ။

Custom Directives — Vue.js

Real-world usage

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

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

Mini challenge

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

Element အပြင်ဘက် click လျှင် callback ခေါ်သော v-click-outside custom directive တစ်ခုရေးပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
vue
<script setup>
const vFocus = {
  mounted(element) {
    element.focus()
  }
}
</script>

<template>
  <label>
    Search
    <input v-focus placeholder="Type to search">
  </label>
</template>

ရလဒ်: Custom directive lifecycle ကိုသုံးရန်

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

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

Quick Quiz 1 / 3

Custom Directives and Plugins ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Props / event → Component + template → Rendered Vue UI
  • DOM behavior အတွက် custom directive နှင့် app-wide capability အတွက် plugin တည်ဆောက်နည်းကို လေ့လာမည်။
  • ရိုးရိုး template logic ကို directive အဖြစ်ရှုပ်ထွေးစေခြင်း
  • Custom Directives and Plugins ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Custom Directives — Vue.js

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

  • ရိုးရိုး template logic ကို directive အဖြစ်ရှုပ်ထွေးစေခြင်း
  • Directive event listener ကို unmounted hook တွင်မရှင်းခြင်း

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

Element အပြင်ဘက် click လျှင် callback ခေါ်သော v-click-outside custom directive တစ်ခုရေးပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Search input receives focus after mount

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

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

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

Custom Directives and Plugins | Thuta Learning