婚庆公司网站建设策划书,WordPress维护模式退出,怀集县住房和城乡规划建设网站,给工厂做代加工文章目录 一、 什么是pinia二、 创建空Vue项目并安装Pinia1. 创建空Vue项目2. 安装Pinia并注册 三、 实现counter四、 实现getters五、 异步action六、 storeToRefs保持响应式解构七、基本使用#xff1a;【1】main.js【2】store》index.js【3】member.ts 一、 什么是pinia
P… 文章目录 一、 什么是pinia二、 创建空Vue项目并安装Pinia1. 创建空Vue项目2. 安装Pinia并注册 三、 实现counter四、 实现getters五、 异步action六、 storeToRefs保持响应式解构七、基本使用【1】main.js【2】store》index.js【3】member.ts 一、 什么是pinia
Pinia 是 Vue 的专属状态管理库可以实现跨组件或页面共享状态是 vuex 状态管理工具的替代品和 Vuex相比具备以下优势
提供更加简单的API 去掉了 mutation 提供符合组合式API风格的API 和 Vue3 新语法统一去掉了modules的概念每一个store都是一个独立的模块搭配 TypeScript 一起使用提供可靠的类型推断
二、 创建空Vue项目并安装Pinia
1. 创建空Vue项目
npm init vuelatest2. 安装Pinia并注册
npm i piniaimport { createPinia } from piniaconst app createApp(App)
// 以插件的形式注册
app.use(createPinia())
app.use(router)
app.mount(#app)三、 实现counter 核心步骤 定义store组件使用store 1- 定义store
import { defineStore } from pinia
import { ref } from vueexport const useCounterStore defineStore(counter, (){// 数据 stateconst count ref(0)// 修改数据的方法 actionconst increment (){count.value}// 以对象形式返回return {count,increment}
})
2- 组件使用store
script setup// 1. 导入use方法import { useCounterStore } from /stores/counter// 2. 执行方法得到store store里有数据和方法const counterStore useCounterStore()
/scripttemplatebutton clickcounterStore.increment{{ counterStore.count }}/button
/template四、 实现getters getters直接使用计算属性即可实现 // 数据state
const count ref(0)
// getter (computed)
const doubleCount computed(() count.value * 2)五、 异步action 思想action函数既支持同步也支持异步和在组件中发送网络请求写法保持一致 步骤 store中定义action组件中触发action 1- store中定义action
const API_URL http://geek.itheima.net/v1_0/channelsexport const useCounterStore defineStore(counter, (){// 数据const list ref([])// 异步actionconst loadList async (){const res await axios.get(API_URL)list.value res.data.data.channels}return {list,loadList}
})2- 组件中调用action
script setupimport { useCounterStore } from /stores/counterconst counterStore useCounterStore()// 调用异步actioncounterStore.loadList()
/scripttemplateulli v-foritem in counterStore.list :keyitem.id{{ item.name }}/li/ul
/template六、 storeToRefs保持响应式解构 直接基于store进行解构赋值响应式数据state和getter会丢失响应式特性使用storeToRefs辅助保持响应式 script setupimport { storeToRefs } from piniaimport { useCounterStore } from /stores/counterconst counterStore useCounterStore()// 使用它storeToRefs包裹之后解构保持响应式const { count } storeToRefs(counterStore)const { increment } counterStore/scripttemplatebutton clickincrement{{ count }}/button
/template七、基本使用
【1】main.js
import { createSSRApp } from vue
import App from ./App.vue// 导入 pinia 实例
import pinia from ./stores
import persist from pinia-plugin-persistedstate
// 使用持久化存储插件
pinia.use(persist)export function createApp() {// 创建 vue 实例const app createSSRApp(App)// 使用 piniaapp.use(pinia)return {app,}
}【2】store》index.js
import { createPinia } from pinia// 创建 pinia 实例
const pinia createPinia()// 默认导出给 main.ts 使用
export default pinia【3】member.ts
import type { LoginResult } from /types/member
import { defineStore } from pinia
import { ref } from vue// 定义 Store
export const useMemberStore defineStore(member,() {// 会员信息const profile refLoginResult()// 保存会员信息登录时使用const setProfile (val: LoginResult) {profile.value val}// 清理会员信息退出时使用const clearProfile () {profile.value undefined}// 记得 returnreturn {profile,setProfile,clearProfile,}},{// 网页端配置// persist: true,// 小程序端配置persist: {storage: {getItem(key) {return uni.getStorageSync(key)},setItem(key, value) {uni.setStorageSync(key, value)},},},},
)