[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
- 基础命令