Blog
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)

HiuZing

🍑
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)
  • 前端面试题

  • JavaScript

  • Vue2

  • port

  • CSS

  • Node.js

  • JavaScript优化

  • uniapp

  • Mini Program

  • TypeScript

  • 面向对象编程

  • UI组件

  • Plugin

  • Vue3

  • 性能优化

  • Axios

  • 状态管理

    • pinia

      • Pinia 初始
      • Pinia state
      • Pinia 解构store
      • Pinia Actions
      • Pinia Getters
      • Pinia API
    • zustand

  • React

  • Mock

  • Icon

  • Template

  • 构建工具

  • 项目规范配置

  • Taro

  • SVG

  • React Native

  • 前端
  • 状态管理
  • pinia
HiuZing
2023-03-25

Pinia state

  1. 访问 state,允许直接修改值的

    <template>
      <div>
        pinia:{{ Test.current }}
        <button @click="change">change</button>
      </div>
    </template>
    
    <script setup lang="ts">
    import {useTestStore} from "./store";
    
    const Test = useTestStore()
    const change = () => {
      Test.current++
    }
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
  2. 改变状态,调用 $patch 方法,批量修改多个值

    <template>
      <div>
        pinia:{{ Test.current }}
        <button @click="change">change</button>
      </div>
    </template>
    
    <script setup lang="ts">
    import {useTestStore} from "./store";
    
    const Test = useTestStore()
    const change = () => {
      Test.$patch({
        current: 888
      })
    }
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
  3. 批量修改函数形式(推荐使用函数形式 可以自定义修改逻辑)。

    任何集合修改(例如,从数组中推送、删除、拼接元素)都需要您创建一个新集合。

    <template>
         <div>
             <button @click="Add">+</button>
              <div>
                 {{Test.current}}
              </div>
              <div>
                {{Test.age}}
              </div>
         </div>
    </template>
     
    <script setup lang='ts'>
    import {useTestStore} from './store'
    const Test = useTestStore()
    const Add = () => {
        Test.$patch((state)=>{
           state.current++;
           state.age = 40
        })
    }
     
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
  4. 通过原始对象修改整个实例

    $state您可以通过将store的属性设置为新对象来替换store的整个状态

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

    <template>
         <div>
             <button @click="Add">+</button>
              <div>
                 {{Test.current}}
              </div>
              <div>
                {{Test.age}}
              </div>
         </div>
    </template>
     
    <script setup lang='ts'>
    import {useTestStore} from './store'
    const Test = useTestStore()
    const Add = () => {
        Test.$state = {
           current:10,
           age:30
        }    
    }
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
  5. 通过actions修改。定义actions在actions中直接使用this就可以指到state里面的值

    // store/index.ts
    import { defineStore } from 'pinia'
    import { Names } from './store-name'
    export const useTestStore = defineStore(Names.TEST, {
         state:()=>{
             return {
                current:1,
                age:30
             }
         },
     
         actions:{
             setCurrent (num:number) {
                 this.current = num
             }
         }
    })
    
    // 使用方法直接在实例调用
    <template>
         <div>
             <button @click="Add">+</button>
              <div>
                 {{Test.current}}
              </div>
              <div>
                {{Test.age}}
              </div>
         </div>
    </template>
    <script setup lang='ts'>
    import {useTestStore} from './store'
    const Test = useTestStore()
    const Add = () => {
         Test.setCurrent(333)
    }
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
上次更新: 2024/08/14, 04:14:33
Pinia 初始
Pinia 解构store

← Pinia 初始 Pinia 解构store→

最近更新
01
React Native 使用SVG
08-13
02
Docker基础命令
08-04
03
算数逻辑单元
07-30
更多文章>
Theme by Vdoing | Copyright © 2021-2024 WeiXiaojing | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式