Pinia 的基本使用

在项目中可能会存在多个页面使用的数据是一样的,在不同页面中需要使用同一数据,这时候我们可以使用本地存储我们要使用的数据,在 vue 中我们有更加灵活和方便来处理这个问题。

Pinia 和 vuex 一样都是用于应用状态的管理,可以在不同页中使用同一数据,用来统一维护。

Pinia 有着两种使用的方式 Option StoreSetup Store,允许你跨组件或页面共享状态。那么 Pinia,相比于 vue2 使用的 vuex 有什么优点呢?

  • 更加符合 vue3 组合式 api,让代码更加扁平化,对 vue2 也做到很好的支持
  • 抛弃了 mutations 的操作,使用 state,getters,actions就能进行状态管理编写,简化了操作。
  • 能更好的支持 TypeScript 类型编写。

使用命令行安装 npm install pinia ,使用如下

js 复制代码
// main.ts
import { createPinia } from 'pinia'
import { createApp } from 'vue'
import App from './App.vue'

createApp(App).use(createPinia()).mount('#app')

OPtion Store

option store 与 Vue2 的选项式 API 类似,可以传入一个带有 stateactionsgetters 属性的 Option 对象。

  • state 用于放置要共享的数据。
  • actions 可以在其中定义函数用于处理一些复杂的处理,例如异步操作。
  • getters 调用可以获取 state 中的数据,并且可以进一步处理所需要的数据。
js 复制代码
// setting.ts
import { defineStore } from "pinia";

export const useSettingStore = defineStore('setting', {
  state: () => ({
    title: '不知名网站',
    theme: '#409eff'
  }),

  getters: {
    titleDescribe: (state) => `${state.title}是该网页的主题名`,
  },

  actions: {
    // 修改主题色
    changeSettingTheme() {
      this.theme = this.theme == '#30B08F' ? '#409eff' : '#30B08F'
    },
    setTitle(title: string) {
      this.title = title
    }
  }
})

// 使用
// home.vue
<el-button :color="settingStore.theme" @click="changeSetting">{{
  settingStore.titleDescribe
}}</el-button>

import { useSettingStore } from "@/stores/setting";
const settingStore = useSettingStore();
const changeSetting = () => {
  settingStore.changeSettingTheme();
  settingStore.setTitle("稀土掘金");
};

Setup Store

setup store 更符合Vue3 的 Composition api的风格,可以直接结合vue3的API定义状态

  • defineStore 用来定义 store。
  • storeToRefs 当我们定义好状态管理,需要使用 storeToRefs 来获取store中的属性和方法,并且可以保持数据的响应式。
js 复制代码
import { defineStore } from "pinia";
import { ref, computed } from 'vue'

export const useSettingStore = defineStore('setting', ()=> {
  const title = ref('不知名网站')
  const theme = ref('#409eff')

  const titleDescribe = computed(() => `${title.value}是该网页的主题名`)

  function changeSettingTheme() {
    theme.value = theme.value == '#30B08F' ? '#409eff' : '#30B08F'
  }

  function setTitle(e: string) {
    title.value = e
  }
  // 传递的函数要 return 返回出来才可以使用
  return { title, theme, titleDescribe, changeSettingTheme, setTitle }
})

// 使用
// home.vue
<el-button :color="settingStore.theme" @click="changeSetting">{{
  settingStore.titleDescribe
}}</el-button>

import { useSettingStore } from "@/stores/setting";
const settingStore = useSettingStore();
const changeSetting = () => {
  settingStore.changeSettingTheme();
  settingStore.setTitle("稀土掘金");
};

// 另一种使用方法
<el-button :color="theme" @click="changeSetting">{{
  titleDescribe
}}</el-button>

import { storeToRefs } from "pinia";
import { useSettingStore } from "@/stores/setting";
const { title, theme, titleDescribe } = storeToRefs(useSettingStore())
const { changeSettingTheme, setTitle } = useSettingStore()
const changeSetting = () => {
  changeSettingTheme();
  setTitle("稀土掘金");
  console.log(title.value)
};

持久化存储数据

我们在页面中修改了状态数据,当刷新页面时会出现一个问题,状态数据变回初始状态,这个时候我们需要做持久化存储数据处理。

借助 pinia-plugin-persist 插件去完成存储数据,做到刷新页面数据不变化,他提供了简便的使用方式以及灵活的配置方式。

npm i pinia-plugin-persist

js 复制代码
// main.ts
import { createPinia } from 'pinia'
import piniaPersistPlugin from 'pinia-plugin-persist'
const pinia = createPinia()
pinia.use(piniaPersistPlugin)
app.use(pinia)
js 复制代码
import { defineStore } from "pinia"
import { ref } from "vue"

export const usePageMonitorStore = defineStore('monitor', () => {

  const libraryTraceVal = ref<number>(0)
  const libraryTraceTime = ref<string>('')

  const abilityTraceVal = ref<number>(0)
  const abilityTraceTime = ref<string>('')

  return { libraryTraceVal, libraryTraceTime, abilityTraceVal, abilityTraceTime }
},
{
  persist: {
    enabled: true,
    strategies: [
      { 
        // 不设置 key 、paths 默认保存全部数据
        // storage 值可为 localStorage 、sessionStorage
        key: 'libraryTraceVal',
        storage: sessionStorage,
        paths: ["libraryTraceVal"]
      }
    ]
  }
}
)
相关推荐
十一吖i2 分钟前
Vue3项目使用ElDrawer后select方法不生效
前端
只可远观3 分钟前
Flutter目录结构介绍、入口、Widget、Center组件、Text组件、MaterialApp组件、Scaffold组件
前端·flutter
周胡杰4 分钟前
组件导航 (HMRouter)+flutter项目搭建-混合开发+分栏效果
前端·flutter·华为·harmonyos·鸿蒙·鸿蒙系统
敲代码的小吉米18 分钟前
前端上传el-upload、原生input本地文件pdf格式(纯前端预览本地文件不走后端接口)
前端·javascript·pdf·状态模式
是千千千熠啊19 分钟前
vue使用Fabric和pdfjs完成合同签章及批注
前端·vue.js
九月TTS1 小时前
TTS-Web-Vue系列:组件逻辑分离与模块化重构
前端·vue.js·重构
我是大头鸟2 小时前
SpringMVC 内容协商处理
前端
Humbunklung2 小时前
Visual Studio 2022 中添加“高级保存选项”及解决编码问题
前端·c++·webview·visual studio
墨水白云2 小时前
nestjs[一文学懂nestjs中对npm功能包的封装,ioredis封装示例]
前端·npm·node.js
满怀10152 小时前
【Vue 3全栈实战】从响应式原理到企业级架构设计
前端·javascript·vue.js·vue