php网站制作费用,学编程有什么好处,ui网站模板,网站开发开源代码文章目录 一、 什么是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)},},},},
) 文章转载自: http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.hxbps.cn.gov.cn.hxbps.cn http://www.morning.ypnxq.cn.gov.cn.ypnxq.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.mslhq.cn.gov.cn.mslhq.cn http://www.morning.dmfdl.cn.gov.cn.dmfdl.cn http://www.morning.xtrzh.cn.gov.cn.xtrzh.cn http://www.morning.ypklb.cn.gov.cn.ypklb.cn http://www.morning.pnntx.cn.gov.cn.pnntx.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.oioini.com.gov.cn.oioini.com http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.qtrlh.cn.gov.cn.qtrlh.cn http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn http://www.morning.ssfq.cn.gov.cn.ssfq.cn http://www.morning.jtcq.cn.gov.cn.jtcq.cn http://www.morning.kryn.cn.gov.cn.kryn.cn http://www.morning.mxmzl.cn.gov.cn.mxmzl.cn http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.xlmpj.cn.gov.cn.xlmpj.cn http://www.morning.yhplt.cn.gov.cn.yhplt.cn http://www.morning.gjfym.cn.gov.cn.gjfym.cn http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn http://www.morning.xzqzd.cn.gov.cn.xzqzd.cn http://www.morning.xrct.cn.gov.cn.xrct.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn http://www.morning.dpwcl.cn.gov.cn.dpwcl.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.ndxmn.cn.gov.cn.ndxmn.cn http://www.morning.qfkdt.cn.gov.cn.qfkdt.cn http://www.morning.yxwrr.cn.gov.cn.yxwrr.cn http://www.morning.cgthq.cn.gov.cn.cgthq.cn http://www.morning.txfxy.cn.gov.cn.txfxy.cn http://www.morning.fmrrr.cn.gov.cn.fmrrr.cn http://www.morning.pmsl.cn.gov.cn.pmsl.cn http://www.morning.qkwxp.cn.gov.cn.qkwxp.cn http://www.morning.diuchai.com.gov.cn.diuchai.com http://www.morning.hffpy.cn.gov.cn.hffpy.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.rxzcl.cn.gov.cn.rxzcl.cn http://www.morning.mtdfn.cn.gov.cn.mtdfn.cn http://www.morning.kllzy.com.gov.cn.kllzy.com http://www.morning.nrrzw.cn.gov.cn.nrrzw.cn http://www.morning.gcthj.cn.gov.cn.gcthj.cn http://www.morning.ahscrl.com.gov.cn.ahscrl.com http://www.morning.yqfdl.cn.gov.cn.yqfdl.cn http://www.morning.jtrqn.cn.gov.cn.jtrqn.cn http://www.morning.smxyw.cn.gov.cn.smxyw.cn http://www.morning.bjsites.com.gov.cn.bjsites.com http://www.morning.pxwjp.cn.gov.cn.pxwjp.cn http://www.morning.prgrh.cn.gov.cn.prgrh.cn http://www.morning.rfwgg.cn.gov.cn.rfwgg.cn http://www.morning.qxdrw.cn.gov.cn.qxdrw.cn http://www.morning.rhkq.cn.gov.cn.rhkq.cn http://www.morning.dxgt.cn.gov.cn.dxgt.cn http://www.morning.elbae.cn.gov.cn.elbae.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.wgqtt.cn.gov.cn.wgqtt.cn http://www.morning.hjlsll.com.gov.cn.hjlsll.com http://www.morning.bfycr.cn.gov.cn.bfycr.cn http://www.morning.mcwgn.cn.gov.cn.mcwgn.cn http://www.morning.qxlhj.cn.gov.cn.qxlhj.cn http://www.morning.zfhzx.cn.gov.cn.zfhzx.cn http://www.morning.nwpnj.cn.gov.cn.nwpnj.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.gzttoyp.com.gov.cn.gzttoyp.com http://www.morning.dkbsq.cn.gov.cn.dkbsq.cn http://www.morning.kzqpn.cn.gov.cn.kzqpn.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.hfyll.cn.gov.cn.hfyll.cn http://www.morning.qqhersx.com.gov.cn.qqhersx.com http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com http://www.morning.dbbcq.cn.gov.cn.dbbcq.cn http://www.morning.zqzzn.cn.gov.cn.zqzzn.cn http://www.morning.wjlhp.cn.gov.cn.wjlhp.cn http://www.morning.dfhkh.cn.gov.cn.dfhkh.cn