Thuta Learning
ရှာဖွေရန်
AdvancedWeb Developmentbeginner

State Management with Pinia

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

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

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

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

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 StatePinia

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

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

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

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

You'll know it worked when: cart.totalItems = 0

State Management with Pinia | Thuta Learning