安裝pinia
1 | npm install --save pinia |
在main.ts引入pinia
1 |
|
Vite Ssr 專案 main.ts引入方法
1 | import { createSSRApp } from 'vue' |
官方寫法:
stores/counter.ts
1 | //引入defineStore |
在頁面使用
- 引入counter的useCounterStore
- 宣告useCounterStore()
引入
宣告
解構
1 | <template> |
pinia 選項寫法 gitHub
pinia 選項寫法 gitHub
改為 Composition API
*重點:
1.引入vue Composition API
2.改為Function 寫法與Composition API
3.必須 return
1 | import { defineStore } from 'pinia'; |
在頁面使用
- 引入counter的useCounterStore
- 宣告useCounterStore()
html 加入 store.命名
使用 store action時 store.addCount()
*重點:
1.store.名稱
2.store.function
1 | <template> |
解構方式引入
1.引入 import {storeToRefs } from ‘pinia’
2.//解構store,數據取出counter, cardLists
const { counter, cardLists } = storeToRefs(store);
3.function 解構 const { fetchApi } = store;
使用
1 | <template> |
範例
stores/user.ts
- acceptHMRUpdatePinia 支援熱模組替換,因此您可以直接在您的應用程式中編輯您的商店並與它們進行交互,而無需重新加載頁面,從而允許您保留現有狀態、添加甚至刪除狀態、操作和 getter。
- 使用$patch修改state 數據
1 | // @ts-check 引入pinia的defineStore與acceptHMRUpdate |
stores/cart.ts
1 | // @ts-check 引入pinia的defineStore與acceptHMRUpdate |
頁面引用
1 | <script setup lang="ts"> |
- reduce
- JavaScript Array Search
- find js
- lastIndexOf
- splice js ;array.splice(索引, 有多少, item1, ....., itemX)
- [JavaScript] slice()、splice()、split() 傻傻分不清
改Composition API寫法
1 | // @ts-check 引入pinia的defineStore與acceptHMRUpdate |
頁面使用
1 | <template> |