首页 > 其他分享 >Pinia

Pinia

时间:2022-10-20 09:01:34浏览次数:55  
标签:const Pinia current state pinia store name

介绍Pinia

Pinia.js 有如下特点:

  • 完整的 ts 的支持;
  • 足够轻量,压缩后的体积只有1kb左右;
  • 去除 mutations,只有 state,getters,actions;
  • actions 支持同步和异步;
  • 代码扁平化没有模块嵌套,只有 store 的概念,store 之间可以自由使用,每一个store都是独立的
  • 无需手动添加 store,store 一旦创建便会自动添加;
  • 支持Vue3 和 Vue2

官方文档Pinia

git 地址 https://github.com/vuejs/pinia

1.起步 安装

yarn add pinia
 
npm install pinia

2.引入注册

Vue3

import { createApp } from 'vue'
import App from './App.vue'
import {createPinia} from 'pinia'
 
const store = createPinia()
let app = createApp(App)
 
 
app.use(store)
 
app.mount('#app')

Vue2

import { createPinia, PiniaVuePlugin } from 'pinia'
 
Vue.use(PiniaVuePlugin)
const pinia = createPinia()
 
new Vue({
  el: '#app',
  // other options...
  // ...
  // note the same `pinia` instance can be used across multiple Vue apps on
  // the same page
  pinia,
})

初始化仓库Store

1.新建一个文件夹store

2.新建文件[name].ts

3.定义仓库Store

import { defineStore } from 'pinia'

// useStore 可以是 useUser、useCart 之类的任何东西
// 第一个参数是应用程序中 store 的唯一 id
export const useStore = defineStore('main', {
    // other options...
})

这个 name,也称为 id,是必要的,Pinia 使用它来将 store 连接到 devtools。 将返回的函数命名为 use... 是跨可组合项的约定,以使其符合你的使用习惯。

使用 store

import { useStore } from '@/stores/counter'

export default {
    setup() {
        const store = useStore()

        return {
            // 您可以返回整个 store 实例以在模板中使用它
            store,
        }
    },
}

State

大多数时候,state 是 store 的核心部分。 我们通常从定义应用程序的状态开始。 在 Pinia 中,状态被定义为返回初始状态的函数。 Pinia 在服务器端和客户端都可以工作。

import { defineStore } from 'pinia'

const useStore = defineStore('storeId', {
    // 推荐使用 完整类型推断的箭头函数
    state: () => {
        return {
            // 所有这些属性都将自动推断其类型
            counter: 0,
            name: '浩然',
        }
    },
})

访问及使用 “state”

默认情况下,您可以通过 store 实例访问状态来直接读取和写入状态:

<template>
<div>pinia:{{countStore.current}}----{{countStore.name}}</div>
<div><button @click="change">pinia++</button></div>
</template>

<script setup lang="ts">
    import { ref, reactive } from 'vue'
    import { useCountStore } from "./store/count";
    const countStore = useCountStore()
    // 可以直接更改state的值
    const change = () => {
        countStore.current++
    }
</script>

重置状态

将状态 重置 到其初始值

const store = useStore()

store.$reset()

批量改变状态

countStore.$patch({
    current: ++countStore.current,
    name : '批量更改'
})

批量修改函数形式

countStore.$patch((state) => {
    state.current++
    state.name = '我被改变了'
})

替换state

通过将其 $state 属性设置为新对象来替换 Store 的整个状态

缺点就是必须修改整个state对象的所有属性

countStore.$state = {
    current: 5,
    name: '修改整个state'
}

通过actions修改state

import {defineStore} from 'pinia'
export const useCountStore = defineStore('count',{
    state: () => {
        return {
            current: 1,
            name: '浩然'
        }
    },
    actions: {
        addCurrent(num) {
            this.current += num
        }
    }
})

使用

<template>
    <div>pinia:{{countStore.current}}----{{countStore.name}}</div>
    <div><button @click="change">pinia++</button></div>
</template>

<script setup lang="ts">
    import { useCountStore } from "./store/count";
    const countStore = useCountStore()
    const change = () => {
        countStore.addCurrent(2)
    }
</script>

订阅状态

通过 store 的 $subscribe() 方法查看状态及其变化,类似于 Vuex 的 subscribe 方法

与常规的 watch() 相比,使用 $subscribe() 的优点是 订阅 只会在 patches 之后触发一次

countStore.$subscribe((args, state) => {
  console.log(args);
  console.log(state);
})

解构store&源码

在Pinia是不允许直接解构是会失去响应性的

const Test = useTestStore()

const { current, name } = Test

console.log(current, name);

差异对比

修改Test current 解构完之后的数据不会变

而源数据是会变的

<template>
<div>origin value {{Test.current}}</div>
<div>
    pinia:{{ current }}--{{ name }}
    change :
    <button @click="change">change</button>
    </div>
</template>

<script setup lang='ts'>
    import { useTestStore } from './store'
    const Test = useTestStore()
    const change = () => {
        Test.current++
    }
    const { current, name } = Test
    console.log(current, name);
</script>

解决方案可以使用 storeToRefs

import { storeToRefs } from 'pinia'
 
const Test = useTestStore()
 
const { current, name } = storeToRefs(Test)

其原理跟toRefs 一样的给里面的数据包裹一层toref

源码 通过toRaw使store变回原始数据防止重复代理

循环store 通过 isRef isReactive 判断 如果是响应式对象直接拷贝一份给refs 对象 将其原始对象包裹toRef 使其变为响应式对象

下载

Actions异步操作

import { defineStore } from 'pinia'
import { Names } from './store-naspace'

type Result = {
    name: string
    isChu: boolean
}

const Login = (): Promise<Result> => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve({
                name: '小满',
                isChu: true
            })
        }, 3000)
    })
}

export const useTestStore = defineStore(Names.TEST, {
    state: () => ({
        user: <Result>{},
        name: "123"
    }),
    actions: {
        async getLoginInfo() {
            const result = await Login()
            this.user = result;
        }
    },
})
<template>
<div>
    <button @click="Add">test</button>
    <div>
        {{Test.user}}
    </div>    
</div>
</template>

<script setup lang='ts'>
    import {useTestStore} from './store'
    const Test = useTestStore()
    const Add = () => {
        Test.getLoginInfo()
    }
</script>
 

订阅Actions的调用

$onAction

只要有actions被调用就会走这个函数

countStore.$onAction((args => {
  console.log(args);
}))

getters

import { defineStore } from 'pinia'
export const useCountStore = defineStore('count', {
    state: () => {
        return {
            current: 1,
            name: '浩然'
        }
    },
    getters: {
        getName(): string {
            return 'getters-' + this.name
        }
    },
})

使用:

<template>
<div>{{countStore.getName}}</div>
</template>

<script setup lang="ts">
    import { useCountStore } from "./store/count";
    const countStore = useCountStore()
</script>

getters相互调用

getters:{
    newCurrent ():number | string {
        return ++this.current + this.newName
    },
        newName ():string {
            return `$-${this.name}`
        }
},

pinia持久化

const __piniaKey = '__PINIAKEY__'
//定义兜底变量


type Options = {
    key?:string
}
//定义入参类型



//将数据存在本地
const setStorage = (key: string, value: any): void => {

    localStorage.setItem(key, JSON.stringify(value))

}


//存缓存中读取
const getStorage = (key: string) => {

    return (localStorage.getItem(key) ? JSON.parse(localStorage.getItem(key) as string) : {})

}

//利用函数柯丽华接受用户入参
const piniaPlugin = (options: Options) => {

    //将函数返回给pinia  让pinia  调用 注入 context
    return (context: PiniaPluginContext) => {

        const { store } = context;

        const data = getStorage(`${options?.key ?? __piniaKey}-${store.$id}`)

        store.$subscribe(() => {

            setStorage(`${options?.key ?? __piniaKey}-${store.$id}`, toRaw(store.$state));

        })

        //返回值覆盖pinia 原始值
        return {

            ...data

        }
    }
}
//初始化pinia
const pinia = createPinia()
//注册pinia 插件
pinia.use(piniaPlugin({

    key: "pinia"

}))

标签:const,Pinia,current,state,pinia,store,name
From: https://www.cnblogs.com/codehaoran/p/16808513.html

相关文章

  • pinia - 为 antdv 表格添加加载状态
    前言我们之前制作的Vue3+AntDesignVue+SpringBoot的增删改查小Demo的功能已经全部实现了,但是还是有一点不完美,在发送请求到后端返回数据这一段时间内前台未做任......
  • vue3 + pinia实现简单购物车功能
    这个小例子是学习vue3与pinia状态管理工具时写的一个简单的购物车功能,它实现了从模拟接口(node.js的json-server提供)读取商品数据,到添加商品到购物车和购物车中删除商品......
  • Vite + Vue3 + Pinia + es6 + TypeScript 搭建项目
    vite中文参考文档:​​https://vitejs.cn/guide/#scaffolding-your-first-vite-project​​执行 npminitvite@latest步骤如下图:下载依赖npmi 启动项目:npmrundev......
  • Vue3 Vite3 状态管理 pinia 基本使用、持久化、在路由守卫中的使用
    在《基于vite创建vue3项目》一文中整合了pinia,有不少伙伴不知道pinia是什么,本文简单介绍pinia。主要包括三方面:pinia的基本用法,在《基于vite创建vue3项目》......
  • 【Vue3.x】pinia
    PiniaVue3中使用Pinia替代vuex更改如下:支持ts体积小,压缩后1KB去除mutations,只有state,getters,actions;去除mutations后,actions直接进行同步和异步操作修改数据去......
  • vue3中pinia的使用总结
      pinia的简介和优势:Pinia是Vue生态里Vuex的代替者,一个全新Vue的状态管理库。在Vue3成为正式版以后,尤雨溪强势推荐的项目就是Pinia。那先来看看Pinia比Vuex好的地方,也......
  • Pinia持久化
    Pinia是Vue的存储库安装1、vue3npminstallpinia--save2、vue2npminstallpinia@vue/composition-api--save引入1、vue3再main.js中import{createPinia......
  • vue3+vant+vue-router+axios+pinia+vite框架搭建
    vue3的官网地址:https://cn.vuejs.org/;这里要说一下,vue3不支持IE11,如果要兼容IE11及其一下,不建议使用vue3。创建vue脚手架,如果你需要使用ts,则需要node版本>=16。本文按照......
  • Vuex新一代状态管理 — Pinia
    最近在学习pinia,做了一些笔记,在这里分享一下:https://note.youdao.com/ynoteshare/index.html?id=3bedfc4d66825be097cee904fe311f56&type=note&_time=1663899586848......
  • Vue:数据仓库配置--Pinia
    pinia作用Pinia是Vue的存储库,它允许您跨组件/页面共享状态。 pinia安装npmipinia 创建文件夹store//store/index.js文件import{defineStore}from......