当前位置: 首页 > 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/263025.html

相关文章:

  • 网站开发前端课程北京seo优化服务
  • 百度站长平台诊断优化网站排名如何
  • 东营 网站建设公司搜索引擎排名google
  • 设计素材图片大全seo网站关键词优化
  • 称心的赣州网站建设青岛关键词排名系统
  • 做网站广告送报纸广告西安seo工作室
  • ECMS做的网站大数据分析营销平台
  • 政务信息公开和网站建设自评餐饮管理培训课程
  • 做图片素材的网站如何免费推广网站
  • 网站影响seo的标签百度电脑版网页版
  • 湘潭做网站找磐石网络一流seo是什么姓氏
  • 在市场部做网站多少工资南昌seo优化公司
  • notepad做网站技巧怎样打百度人工客服热线
  • 网站转应用互联网营销师有什么用
  • 网站安全维护公司seo积分优化
  • 深圳网站设计|优选灵点网络怎么注册自己公司的网址
  • 域名注册解析管理网站seo站长工具推广平台
  • 网站开发filter佛山网站建设维护
  • 成都建设网站报价公司建立网站的步骤
  • 网站移动端是什么情况看b站二十四小时直播间
  • 创新的菏泽网站建设优化的意思
  • 宝安住房和建设局网站推广营销是什么
  • 网站上传后徐州自动seo
  • 打码网站做的比较好的是哪些营销比较成功的品牌
  • 简要叙述如何规划建设一个企业网站营销推广活动策划方案
  • 大气装饰装修企业网站模版源码保定seo建站
  • 海口市建设工程质量安全监督站网站海口网站排名提升
  • 企业网站的建设企业百度竞价托管费用
  • 天津网站制作的公司外贸推广平台有哪几个
  • 渝北集团网站建设网络营销公司怎么注册