[TOC]
# Pinia Store
## 概括了 Pinia 的核心概念與用法
| 概念 | 作用 | 用法 |
|---------| ------------------------ | -----------------|
| State | 存儲數據 | state: () => ({ count: 0 })|
|Getters | 從 State 派生數據 |getters: { doubleCount() { return this.count * 2 }}|
|Actions |處理異步操作 |actions: { increment() { setTimeout(() => { this.count++ }, 1000) }}|
|Mutations| 更改 State 的唯一方法 |mutations: { increment() { this.count++ }}|
|defineStore| 定義 Store |export const useCounter = defineStore({<br> state: () => ... <br> getters: ..., <br> actions: ..., <br> mutations: ...<br>})|
構建邊界:
- 必須安裝 pinia 依賴
- 創建 Store 文件,如 store/user.js
- 使用 defineStore() 定義 Store
- 在 Store 中設置 state、getters、actions 和 mutations
- 在根組件或入口文件中安裝使用該 Store
開發流程:
1. 安裝 `pinia: npm install pinia`
2. 創建 store/user.js 文件:
```
js
import { defineStore } from 'pinia'
export const useUserStore = defineStore({
// store id
id: 'user',
// state
state: () => ({
name: 'Evan'
}),
// getters
getters: {
doubleName() {
return this.name + this.name
}
},
// actions
actions: {
setName(name) {
this.name = name
}
},
// mutations
mutations: {
CHANGE_NAME(state, name) {
state.name = name
}
}
})
```
3. 在 src/main.js 中安裝 Store:
```
js
import { createPinia } from 'pinia'
import { useUserStore } from '@/store/user'
const pinia = createPinia()
app.use(pinia)
```
4. 在組件中使用該 Store:
```
js
export default {
setup() {
const user = useUserStore()
user.name
user.doubleName
user.setName('Jack')
}
}
```
示例使用:
使用 Pinia 的 Table Store,可以構建一個簡單表格:
```
js
// TableStore.js
export const useTableStore = defineStore({
state: () => ({
tableData: [
{ id: 1, name: 'Jack' },
{ id: 2, name: 'John' }
]
}),
getters: {
tableRows() {
return this.tableData.map(row => `
<tr>
<td>${row.id}</td>
<td>${row.name}</td>
</tr>
`).join('')
}
}
})
```
在組件中:
```
html
<table>
<tbody>
<tr v-html="tableRows" /> <!-- 使用 getters 中的 tableRows -->
</tbody>
</table>
<script>
import { useTableStore } from '@/store/TableStore'
export default {
setup() {
const table = useTableStore()
return {
tableRows: table.tableRows
}
}
}
</script>
```
## Pinia 支持一些比較高級的使用法,可以構建更加復雜和強大的狀態管理。
主要有以下幾點:
### 1. 嵌套 Store
:在一個 Store 中使用其他 Store,構建層級關系。
```
js
// store/user.js
import { useAddressStore } from './address'
export const useUserStore = defineStore({
state: () => ({
// 使用 address Store
address: useAddressStore()
}),
// ...
})
```
### 2. 模塊化 Store
:將一個 Store 分割為多個模塊,更加清晰。
```
js
// store/user.js
export const userStore = defineStore({
state: () => ({ ... }),
modules: {
info: {
state: () => ({ ... }),
getters: { ... },
actions: { ... }
},
prefs: {
// ...
}
}
})
```
### 3. 網絡狀態管理
在 Store 中添加 state.isLoading 等屬性,統一管理網絡狀態。
```
js
export const userStore = defineStore({
state: () => ({
isLoading: false,
user: null,
error: null
}),
actions: {
async getUser() {
this.isLoading = true
this.error = null
try {
const res = await fetchUser()
this.user = res.data
} catch (err) {
this.error = err
} finally {
this.isLoading = false
}
}
}
})
```
### 4. 訪問其他 Store
:在 action 或 mutation 中,可以訪問其他 Store 并觸發方法。
```
js
// store/user.js
import { cartStore } from './cart'
export const userStore = defineStore({
actions: {
checkout() {
// 訪問 cartStore 并調用其方法
cartStore.placeOrder()
}
}
})
```
### 5. 訂閱 Store 更新
:在其他組件或 Store 中,可以訂閱某個 Store 的更新。
```
js
// 訂閱 userStore
const unsubscribe = userStore.subscribe(
// 回調函數,當 userStore 更新時被調用
() => { /** ... **/ }
)
// 取消訂閱
unsubscribe()
```
掌握 Pinia 這些高級用法,可以讓我們構建出模塊化、可擴展和健壯的狀態管理解決方案。如果您遇到使用 Pinia 構建復雜應用時的疑問,歡迎提出。我可以提供更加全面的指導與建議。
- 系統設計
- 需求分析
- 概要設計
- 詳細設計
- 邏輯模型設計
- 物理模型設計
- 產品設計
- 數據驅動產品設計
- 首頁
- 邏輯理解
- 微服務架構的關系數據庫優化
- Java基礎架構
- 編程范式
- 面向對象編程【模擬現實】
- 泛型編程【參數化】
- 函數式編程
- 響應式編程【異步流】
- 并發編程【多線程】
- 面向切面編程【代碼復用解耦】
- 聲明式編程【注解和配置】
- 函數響應式編程
- 語法基礎
- 包、接口、類、對象和切面案例代碼
- Springboot按以下步驟面向切面設計程序
- 關鍵詞
- 內部類、匿名類
- 數組、字符串、I/O
- 常用API
- 并發包
- XML
- Maven 包管理
- Pom.xml
- 技術框架
- SpringBoot
- 項目文件目錄
- Vue
- Vue項目文件目錄
- 遠程組件
- 敏捷開發前端應用
- Pinia Store
- Vite
- Composition API
- uniapp
- 本地方法JNI
- 腳本機制
- 編譯器API
- 注釋
- 源碼級注釋
- Javadoc
- 安全
- Swing和圖形化編程
- 國際化
- 精實或精益
- 精實軟件數據庫設計
- 精實的原理與方法
- 項目
- 零售軟件
- 擴展
- 1001_docker 示例
- 1002_Docker 常用命令
- 1003_微服務
- 1004_微服務數據模型范式
- 1005_數據模型
- 1006_springCloud
- AI 流程圖生成
- Wordpress_6
- Woocommerce_7
- WooCommerce常用的API和幫助函數
- WooCommerce的鉤子和過濾器
- REST API
- 數據庫API
- 模板系統
- 數據模型
- 1.Woo主題開發流程
- Filter
- Hook
- 可視編輯區域的函數工具
- 渲染字段函數
- 類庫和框架
- TDD 通過測試來驅動開發
- 編程范式對WordPress開發
- WordPress和WooCommerce的核心代碼類庫組成
- 數據庫修改
- 1.WP主題開發流程與時間規劃
- moho
- Note 1
- 基礎命令