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

State Management with Pinia

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

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

  • Pinia store တည်ဆောက်ရန်
  • State, getters နှင့် actions ခွဲရန်
  • Store ကို component တွင်သုံးရန်

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

State Management with Pinia ကို ရိုးရိုးလေးနားလည်မယ်

Local component state ကို အလွယ်တကူမရွှေ့ပါနှင့်။ User session, cart သို့မဟုတ် app-wide filters ကဲ့သို့ component များစွာမျှဝေသည့် state အတွက် Pinia သုံးပါ။ State ကို function ဖြင့်ကြေညာပြီး derived state ကို getters, changes ကို actions အဖြစ်ရေးပါ။

Problem → Why → Solution

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

App တွင် Pinia plugin register မလုပ်ခြင်း

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

Component-local state အားလုံးကို global store ထဲထည့်ခြင်း

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

Component များစွာမျှဝေသုံးသော state ကို Pinia store ဖြင့် စနစ်တကျစီမံမည်။

Mental Model
  1. Action / shared data
  2. Reactive store
  3. Subscribed component

Pinia store တည်ဆောက်ရန်

အဓိက Syntax / Pattern

javascript
import { defineStore } from 'pinia'

export const useCartStore = defineStore('cart', {
  state: () => ({ items: [] }),
  getters: {
    totalItems: (state) => state.items.length
  },
  actions: {
    add(product) {
      this.items.push(product)
    }
  }
})

Component များစွာမျှဝေသုံးသော state ကို Pinia store ဖြင့် စနစ်တကျစီမံမည်။

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

  1. Action / shared dataimport { defineStore } from 'pinia'

    Local component state ကို အလွယ်တကူမရွှေ့ပါနှင့်။ User session, cart သို့မဟုတ် app-wide filters ကဲ့သို့ component များစွာမျှဝေသည့် state အတွက် Pinia သုံးပါ။ State ကို function ဖြင့်ကြေညာပြီး derived state ကို getters, changes ကို actions အဖြစ်ရေးပါ။

  2. Reactive storeactions: {

    Component များစွာမျှဝေသုံးသော state ကို Pinia store ဖြင့် စနစ်တကျစီမံမည်။

  3. Subscribed component})

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

Execution Flow
  1. Action / shared data
  2. Reactive store
  3. Subscribed component

Success

Pinia store တည်ဆောက်ရန်

Failure

App တွင် Pinia plugin register မလုပ်ခြင်း

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

Local component state ကို အလွယ်တကူမရွှေ့ပါနှင့်။ User session, cart သို့မဟုတ် app-wide filters ကဲ့သို့ component များစွာမျှဝေသည့် state အတွက် Pinia သုံးပါ။ State ကို function ဖြင့်ကြေညာပြီး derived state ကို getters, changes ကို actions အဖြစ်ရေးပါ။

javascript
import { defineStore } from 'pinia'

export const useCartStore = defineStore('cart', {
  state: () => ({ items: [] }),
  getters: {
    totalItems: (state) => state.items.length
  },
  actions: {
    add(product) {
      this.items.push(product)
    }
  }
})
You should see
cart.totalItems = 0

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

User preferences အတွက် theme နှင့် locale state ပါသော Pinia store တစ်ခုတည်ဆောက်ပါ။

Pinia State — Pinia

Real-world usage

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

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

Mini challenge

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

User preferences အတွက် theme နှင့် locale state ပါသော Pinia store တစ်ခုတည်ဆောက်ပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
javascript
import { defineStore } from 'pinia'

export const useCartStore = defineStore('cart', {
  state: () => ({ items: [] }),
  getters: {
    totalItems: (state) => state.items.length
  },
  actions: {
    add(product) {
      this.items.push(product)
    }
  }
})

ရလဒ်: Pinia store တည်ဆောက်ရန်

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

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

Quick Quiz 1 / 3

State Management with Pinia ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Action / shared data → Reactive store → Subscribed component
  • Component များစွာမျှဝေသုံးသော state ကို Pinia store ဖြင့် စနစ်တကျစီမံမည်။
  • App တွင် Pinia plugin register မလုပ်ခြင်း
  • State Management with Pinia ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Pinia State — Pinia

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

  • App တွင် Pinia plugin register မလုပ်ခြင်း
  • Component-local state အားလုံးကို global store ထဲထည့်ခြင်း

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

User preferences အတွက် theme နှင့် locale state ပါသော Pinia store တစ်ခုတည်ဆောက်ပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: cart.totalItems = 0

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

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

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

State Management with Pinia | Thuta Learning