Pinia介紹

大綱

Pinia簡介

  • 可跨元件/頁面共享狀態
  • 支援Vue2和Vue3
  • TypeScript支援
  • Actions 支援同步和非同步
  • 移除了Mutations

Pinia&Vuex

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Pinia架構

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

安裝

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

掛載

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

創建Store

CPMPF/src/ store

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

定義Store

引入defineStore

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Store ID

Store ID 必須為唯一值

如果ID 重複,在同一個Vue元件/ TS
檔裡定義Store 實例變數的時候,會以
先定義的為有效值,後續定義的store不
會覆蓋先前定義的store

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Store範例

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

元件引入Pinia

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

取用state

Components引入完
此時store已經是proxy的格式,直接”userStore.”就可以取出變數或方法

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

storeToRefs 解構

  • 透過解構來生省略用時”userStore.”的字眼
  • state、getters需透過pinia的storeToRefs方法來解構(若用一般方法解構會失去響應式的功能)
  • Store裡的function可以用一般解構方式解構

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

$reset初始化

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

$patch 批量修改(物件形式)

  • 以賦值的方式來修改值
  • $patch須帶入一個物件,物件裡寫欲修改的變數與值
  • 可修改一至多個變數

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

$patch 批量修改(函數形式)

  • 可以對引用的變數加以修改
  • 可修改一至多個變數

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

$subscribe 訂閱

  • 監聽state的變化
  • 若state有被修改則會觸發$subscribe

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Store寫法差異

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

@yu736