Vue3中使用Pinia修改State的五种方式

 更新时间:2023年11月15日 15:03:51   作者:xyphf_和派孔明  
这篇文章主要介绍了Vue3中使用Pinia修改State的五种方式,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧

修改Pinia仓库的值有5种方式

src/store/index.ts

import { defineStore } from 'pinia';
import { Names } from './store-name';
export const useTestStore = defineStore(Names.Test, {
    state:()=>{
        return {
            current:1111,
            name: '小满111'
        }
    },
    getters:{ // 类似computed计算属性 同样有缓存的
    },
    actions:{ // 类似 methods方法 可以做同步、异步操作 提交state
    }
});

第一种修改State的方式

<template>
    <div>
      pinia: {{ Test.current }} --- {{ Test.name }}
      <button @click="change">change</button>
    </div>
</template>
<script setup lang="ts">
import  {useTestStore} from './store';
const Test = useTestStore();
// 第一种修改State的方式
const change = () => {
   Test.current++
}
</script>
<style scoped>
</style>

第二种修改State的方式

<template>
    <div>
      pinia: {{ Test.current }} --- {{ Test.name }}
      <button @click="change">change</button>
    </div>
</template>
<script setup lang="ts">
import  {useTestStore} from './store';
const Test = useTestStore();
// 第二种修改State的方式 批量修改对象属性
const change = () => {
  Test.$patch({
    current: 999,
    name: '李四'
  })
}
</script>
<style scoped>
</style>

第三种修改State的方式

<template>
    <div>
      pinia: {{ Test.current }} --- {{ Test.name }}
      <button @click="change">change</button>
    </div>
</template>
<script setup lang="ts">
import  {useTestStore} from './store';
const Test = useTestStore();
// 第三种修改State的方式 向$patch中传入工厂函数 可以写逻辑
const change = () => {
  Test.$patch((state) => { 
    if(state.current>1113){
      state.current--;
      state.name = '罗翔老师';
    } else {
      state.current++;
      state.name = '罗永浩老师';
    }
  })
}
</script>
<style scoped>
</style>

第四种修改State的方式

<template>
    <div>
      pinia: {{ Test.current }} --- {{ Test.name }}
      <button @click="change">change</button>
    </div>
</template>
<script setup lang="ts">
import  {useTestStore} from './store';
const Test = useTestStore();
// 第四种修改State的方式 覆盖这个state对象
const change = () => {
  Test.$state = {
    current: 1024,
    name: '罗城将军'
  }
}
</script>
<style scoped>
</style>

第五种修改State的方式

调用actions里面的方式

在 src/store/index.ts 里面的actions里面写个方法

import { defineStore } from 'pinia';
import { Names } from './store-name';
export const useTestStore = defineStore(Names.Test, {
    state:()=>{
        return {
            current:1111,
            name: '小满111'
        }
    },
    getters:{ // 类似computed计算属性 同样有缓存的
    },
    actions:{ // 类似 methods方法 可以做同步、异步操作 提交state
        setCurrent(num:number){ // 注意此处不要写箭头函数,否则this指向就不对了
            this.current = num;
        }
    }
});

再在组件里面调用

<template>
    <div>
      pinia: {{ Test.current }} --- {{ Test.name }}
      <button @click="change">change</button>
    </div>
</template>
<script setup lang="ts">
import  {useTestStore} from './store';
const Test = useTestStore();
// 第五种 
const change = () => {
  Test.setCurrent(5173);  // 直接调用setCurrent函数
}
</script>
<style scoped>
</style>

到此这篇关于Vue3中使用Pinia修改State的方法的文章就介绍到这了,更多相关vue3使用Pinia修改state内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • uniapp开发打包成H5部署到服务器的详细步骤

    uniapp开发打包成H5部署到服务器的详细步骤

    前端使用uniapp开发项目完成后,需要将页面打包,生成H5的静态文件,部署在服务器上这样通过服务器链接地址,直接可以在手机上点开来访问,下面小编给大家讲解uniapp开发打包成H5部署到服务器的步骤,感兴趣的朋友一起看看吧
    2022-11-11
  • Vue项目如何根据图片url获取file对象并用axios上传

    Vue项目如何根据图片url获取file对象并用axios上传

    这篇文章主要介绍了Vue项目如何根据图片url获取file对象并用axios上传问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • Vue程序化的事件监听器(实例方案详解)

    Vue程序化的事件监听器(实例方案详解)

    本文通过两种方案给大家介绍了Vue程序化的事件监听器,每种方案通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2020-01-01
  • 分析Vue指令实现原理

    分析Vue指令实现原理

    自定义指令是vue中使用频率仅次于组件,其包含bind、inserted、update、componentUpdated、unbind五个生命周期钩子。本文将对vue指令的工作原理进行相应介绍
    2021-06-06
  • Vue中的字符串模板的使用

    Vue中的字符串模板的使用

    本篇文章主要介绍了Vue中的字符串模板的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Vue实现计算器计算效果

    Vue实现计算器计算效果

    这篇文章主要为大家详细介绍了Vue实现计算器计算效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • vue-cli 3.0 版本与3.0以下版本在搭建项目时的区别详解

    vue-cli 3.0 版本与3.0以下版本在搭建项目时的区别详解

    这篇文章主要介绍了vue-cli 3.0 版本与3.0以下版本在搭建项目时的区别详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • 前端axios取消请求总结详解

    前端axios取消请求总结详解

    这篇文章主要为大家介绍了前端axios取消请求总结示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • 解析Vue2 dist 目录下各个文件的区别

    解析Vue2 dist 目录下各个文件的区别

    本篇文章主要介绍了解析Vue2 dist 目录下各个文件的区别,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • vue中的vue-print-nb如何实现页面打印

    vue中的vue-print-nb如何实现页面打印

    这篇文章主要介绍了vue中的vue-print-nb如何实现页面打印,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04

最新评论