当前位置: 首页 > wzjs >正文

公司部门解散员工赔偿中国优化网

公司部门解散员工赔偿,中国优化网,口碑营销优化推广,网站备案 新网官方文档:使用 Pinia 进行状态管理 |Quasar 框架 视频教程:quasar框架store-状态管理库pinia介绍_哔哩哔哩_bilibili 使用 Quasar CLI 创建一个新的store quasar new store date --format jsPinia存储模板详解解 基本结构解析 import { defineStore,…

官方文档:使用 Pinia 进行状态管理 |Quasar 框架
视频教程:quasar框架store-状态管理库pinia介绍_哔哩哔哩_bilibili

使用 Quasar CLI 创建一个新的store

quasar new store date --format js

Pinia存储模板详解解

基本结构解析

import { defineStore, acceptHMRUpdate } from 'pinia'// 定义存储
export const useMyStore = defineStore('myStore', {state: () => ({}),  // 状态定义getters: {},        // 计算属性actions: {}         // 操作方法
})// HMR (热模块替换)支持
if (import.meta.hot) {import.meta.hot.accept(acceptHMRUpdate(useMyStore, import.meta.hot))
}

如何扩展模板

1. 定义状态 (state)

state 是存储的核心数据部分:

state: () => ({count: 0,user: null,items: [],loading: false
})

2. 添加 getters

getters 类似于计算属性,用于派生状态:

getters: {doubleCount: (state) => state.count * 2,isAuthenticated: (state) => state.user !== null,activeItems: (state) => state.items.filter(item => item.active)
}

3. 添加 actions

actions 用于修改状态和执行异步操作:

actions: {increment() {this.count++},async fetchUser(userId) {this.loading = truetry {const response = await api.getUser(userId)this.user = response.data} catch (error) {console.error('Failed to fetch user:', error)} finally {this.loading = false}},reset() {this.$reset() // 内置方法,重置到初始状态}
}

[!INFO]
Pinia也支持类似Vue3的组合式API写法

在组件中使用 Pinia Store

在 Vue 组件中使用 Pinia Store 非常简单,以下是几种常见的使用方式:

在组件 setup 中使用

<script setup>
import { useCounterStore } from '@/stores/counter'// 在setup中获取store实例
const counterStore = useCounterStore()// 访问状态
console.log(counterStore.count)// 使用getter
console.log(counterStore.doubleCount)// 调用action
function increment() {counterStore.increment()
}
</script><template><div><p>Count: {{ counterStore.count }}</p><p>Double: {{ counterStore.doubleCount }}</p><button @click="increment">Increment</button></div>
</template>

使用 storeToRefs 解构保持响应式

<script setup>
import { useCounterStore } from '@/stores/counter'
import { storeToRefs } from 'pinia'const counterStore = useCounterStore()// 使用storeToRefs解构可以保持响应式
const { count, doubleCount } = storeToRefs(counterStore)
// actions不需要解构,可以直接从store调用
const { increment } = counterStore
</script><template><div><p>Count: {{ count }}</p><p>Double: {{ doubleCount }}</p><button @click="increment">Increment</button></div>
</template>

选项式 API 中使用

如果你使用的是选项式 API,可以在 setup() 中使用:

<script>
import { useCounterStore } from '@/stores/counter'export default {setup() {const counterStore = useCounterStore()return { counterStore }},methods: {increment() {this.counterStore.increment()}}
}
</script><template><div><p>Count: {{ counterStore.count }}</p><button @click="increment">Increment</button></div>
</template>

在模板中直接使用

<script setup>
import { useCounterStore } from '@/stores/counter'
const counter = useCounterStore()
</script><template><div><!-- 直接访问store属性 --><h1>{{ counter.count }}</h1><!-- 直接调用action --><button @click="counter.increment()">+</button><button @click="counter.decrement()">-</button><!-- 使用getter --><p>Double: {{ counter.doubleCount }}</p></div>
</template>

监听状态变化

<script setup>
import { useCounterStore } from '@/stores/counter'
import { watch } from 'vue'const counter = useCounterStore()// 监听count的变化
watch(() => counter.count,(newValue, oldValue) => {console.log(`Count changed from ${oldValue} to ${newValue}`)}
)// 或者使用store的$subscribe方法
counter.$subscribe((mutation, state) => {console.log('Store changed:', mutation, state)
})
</script>

重置 store 状态

<script setup>
import { useCounterStore } from '@/stores/counter'
const counter = useCounterStore()function reset() {counter.$reset() // 重置到初始状态
}
</script><template><button @click="reset">Reset Store</button>
</template>

最佳实践建议

  1. 命名一致性:store 变量命名建议使用 xxxStore 格式,如 userStore, productStore
  2. 避免直接修改状态:尽量通过 actions 修改状态,而不是直接赋值
  3. 合理使用解构:使用 storeToRefs 解构状态和 getters,但不要解构 actions
  4. 模块化:将大型应用的状态拆分到多个 store 中
  5. TypeScript:如果使用 TS,可以为 store 添加类型定义以获得更好的类型支持
http://www.dtcms.com/wzjs/827524.html

相关文章:

  • 校园网站建设系统设计服务器怎么做看视频的网站
  • asp 大型网站开发开发网站公司地址
  • flash网站系统公众号商城怎么开
  • 记事本做网站怎么插图软文营销推广
  • wordpress 图片站软件开发工程师薪资待遇
  • 上海模板网建站移动网站怎么做
  • 团购网站怎么做企业网站建设效益分析
  • ui设计师作品集网站免费网站建设垂询186 6159 6345
  • 无锡专业做网站的公司哪家好深圳市住房和建设局门户网站
  • 萧山做网站的企业上海巴士公司
  • 建设网站需要哪个语言编译器管理课程培训视频教程全集
  • 网络推广需要花多少钱引擎优化
  • 门户网站的自身的特性市场营销策略的概念
  • 网站关闭申请书中国手机网站
  • 推荐几个没封的网站2021货源一件代发从哪里找
  • 石家庄企业商城版网站建设做优化关键词
  • 网站制作公司怎么收费wordpress食品模板下载
  • 公司手机网站建设公司工程机械网
  • 深圳建设工程交易服务网站个人网站成品下载
  • 网站流量数据分析互联网开发技术有哪些
  • 网站建设 服务流程网站公司深圳
  • 昆明网站建设公司哪里有市场营销培训班
  • 响应式网站建设系统开票开网站建设费
  • 百度搜索引擎网站京北网app下载
  • 柳州市建设投资开发公司网站何苦做游戏网站
  • 万网网站模板购买大气装饰公司网站源码
  • 免费国外网站北京广告设计制作公司
  • 濮阳网站建设电话asp婚纱摄影网站
  • 做网站为什么用phpseo流量工具
  • 做外贸业务去哪些网站做网站只开发手机端可不可以