• pinia学习


    pinia状态管理

    名字来源

    Pinia是西班牙语中菠萝-词最相似的英语发音: pina。菠萝实际上是一组单独的果实, 它们结合在一起形成一 个水果。与store类似, 每个store都是 单独存在的,但它们最终都是相互关联的。它也是一种美味的热带水果, 原产于南美洲。

    状态:

    Pinia从2019年11月左右,开始尝试重新使用Composition API设计Vue Store.
    Pinia试图尽可能接近Vuex的理念,旨在测试Vuex下- -次迭代的一个方案。目前,Vuex 5的open RFC, API与
    Pinia的API非常相似,这说明Pinia成功 了。
    注意, Pinia的作者(Eduardo), 也是Vue.js核心团队的一员,积极参与Router和Vuex等API的设计。设计Pinia 的
    目的是想重新设计使用store的体验,同时保持Vue的容易理解的理念。将Pinia的API与Vuex保持尽可能的接近,
    因为它不断向前发展,使人们能够方便地迁移到Vuex,甚至在未来融合两个项目。

    特性:

    Pinia具有以下几点特性:
    ●. 直观,像定义components- 样地定义store
    ●  完整的Typescript支持
    ●  去除mutations, 只有state, getters, actions
    ●  actions支持同步和异步
    ●  Vue Devtools支持Pinia, 提供更好的开发体验
    ●  能够构建多个stores,实现自动地代码拆分
    ●  极其轻量(1kb) ,甚至感觉不到它的存在

     使用方式

    npm i pinia

     在main.ts中 引入pinia 结构出 createPInia方法  app上use方法调用 createPInia方法

    1. import { createApp } from 'vue'
    2. // import './style.css'
    3. import App from './App.vue'
    4. // 1.安装pinia-> npm install pinia
    5. // 2.导入pinia
    6. import { createPinia } from 'pinia'
    7. createApp(App).use(createPinia()).mount('#app')

    创建store库

    新建store文件夹 在index.ts中书写如下

    1. 从 pinia中 结构拿出defineStore 
    2. 调用defineStore 并导出
    3. 第一个参数为store名字,第二个参数为对象
    4. 在第二个参数对象中 书写state action getter module

    1. import { defineStore } from 'pinia'
    2. export default defineStore('main', {
    3. state() {
    4. return {
    5. count: 10,
    6. list: [{
    7. name: "iphone13",
    8. price: 5880,
    9. num: 1
    10. }, {
    11. name: "MetaPro",
    12. price: 6880,
    13. num: 1
    14. }]
    15. }
    16. },
    17. })

    页面使用

    使用 state

    1. 引入导出的仓库函数     ( 这里是:useMainStore )

    2. 调用函数                       ( 这里做了赋值 useMainStore() )

    3. 也是可以直接通过           {{store.xxx}} 来渲染数据

    4. 结构需要从 pinia中拿到 storeToRefs方法,把store转成响应式


    修改方式:

    1. 直接store. xxx 进行修改;如果是结构的数据需要去写他的 .value(因为它是个对象)
    2. $patch 方法修改 (方法内部需要传对象)
    3. $patch 方法执行回调函数 传入state对象

    重置状态:是调用 store.$reset() 方法

     页面部分:

    1. <template>
    2. <div>
    3. <h1>商品总价:{{ store.sumPrice }}h1>
    4. <h1>商品总价:{{ sumPrice }}h1>
    5. <h1>商品数量:{{ store.count }}h1>
    6. <h1>商品数量:{{ count }}h1>
    7. <h1>商品列表h1>
    8. <ul>
    9. <li v-for="item in store.list">{{ item.name }}li>
    10. ul>
    11. <button @click="handleClick">+1button>
    12. <button @click="patchClick">+5button>
    13. <button @click="patchFnClick">+10button>
    14. <button @click="toggleState">替换statebutton>
    15. <button @click="resetState">重置状态button>
    16. <button @click="store.getTimu()">获取异步数据button>
    17. <h1>题目列表h1>
    18. <ul>
    19. <li v-for="item in store.timuList">
    20. {{ item.quiz }}
    21. li>
    22. ul>
    23. div>
    24. template>
    25. <style>
    26. #app {
    27. font-family: Avenir, Helvetica, Arial, sans-serif;
    28. -webkit-font-smoothing: antialiased;
    29. -moz-osx-font-smoothing: grayscale;
    30. text-align: center;
    31. color: #2c3e50;
    32. margin-top: 60px;
    33. }
    34. style>

     index.ts部分

    1. import { defineStore } from "pinia";
    2. import axios from 'axios';
    3. export default defineStore('main', {
    4. state: () => {
    5. return {
    6. count: 10,
    7. list: [{
    8. name: "iphone13",
    9. price: 5880,
    10. num: 1
    11. }, {
    12. name: "MetaPro",
    13. price: 6880,
    14. num: 1
    15. }],
    16. timuList: [] as any
    17. }
    18. },
    19. getters: {
    20. sumPrice: (state) => {
    21. return state.list.reduce((pre, item) => {
    22. return pre + (item.price * item.num)
    23. }, 0)
    24. }
    25. },
    26. // 同步和异步皆可以
    27. actions: {
    28. async getTimu() {
    29. // let result = axios.get('https://www.cpengx.cn/static/timu/timu.json')
    30. let result = axios.get('/timu.json')
    31. console.log(result)
    32. this.timuList = (await result).data;
    33. }
    34. }
    35. })

    getters:

    在store仓库中 和state同级 ,写getters对象 在对象中写方法 要求传入 state


    在页面使用:

    1. 可通过store. xxx 的方式 ;
    2. 结构(需要把store转成响应式)

    actions:

     actions同步异步都可以

    在store仓库中 和state同级 ,写aciton对象 在对象中写方法 


    在页面使用:

    1. 可通过store. xxx 的方式 ;
    2. 结构(需要把store转成响应式)
  • 相关阅读:
    【Rancher】Rancher 2.6.x 搭建Kubernetes集群
    Linux shell编程学习笔记51: cat /proc/cpuinfo:查看CPU详细信息
    【Java 进阶篇】JSP EL 详解
    python写一个开机启动的选项
    jetpac--navigation
    【Harmony OS】【ArkUI】ets开发 简易视频播放器
    django学习记录07——订单案例(复选框+ajax请求)
    基于SSM的视频点播系统设计与实现
    FluentValidation在C# WPF中的应用
    动态调用微服务
  • 原文地址:https://blog.csdn.net/weixin_60968779/article/details/127520504