Pinia+Router学习笔记(二)

简介: 本节记录修改State中值的五种方式
书接上回,我们已经定义好了一个store,接下来介绍修改state中值的五种方式

写在前面:在TypeScript中,修改后的值的类型必须和修改之前的值类型相同,否则会报类型错误
第一种:直接修改,简单粗暴

<template>
    <div>pinia:{{ Test.current }}---{{ Test.name }}</div>
</template>

<script setup lang="ts">
import { useTestStore } from './Store'

const Test = useTestStore()
// 直接修改Test
Test.current = '我是第一种修改方式'
Test.num = '开始你的炸弹秀'    // 报错,提示不能将string类型的值赋给number类型
</script>

<style scoped></style>

第二种:通过$patch方法传入一个对象进行修改(可批量修改)

<template>
    <div>pinia:{{ Test.current }}---{{ Test.name }}</div>
</template>

<script setup lang="ts">
import { useTestStore } from './Store'

const Test = useTestStore()
// $patch+对象
Test.$patch({
    current: '我是第二种修改方式',
  num:888,
})
</script>

<style scoped></style>

注意:通过这种$patch方法+对象的修改方式只会修改传入对象中对应的属性,state中的其他属性不受影响<br />第三种:还是$patch,但是传入一个回调函数(注意带上state参数),此方法相比对象更加灵活,推荐使用

<template>
    <div>pinia:{{ Test.current }}---{{ Test.name }}---{{ Test.num }}</div>
</template>

<script setup lang="ts">
import { useTestStore } from './Store'

const Test = useTestStore()
Test.$patch((state) => {
    state.current = '我是第三种修改方式'
    state.num = 666
})
</script>

<style scoped></style>

第四种:通过Test身上的$state属性直接覆盖整个state,此方法存在一定风险,请谨慎使用

<template>
    <div>pinia:{{ Test.current }}---{{ Test.name }}---{{ Test.num }}</div>
</template>

<script setup lang="ts">
import { useTestStore } from './Store'

const Test = useTestStore() 
Test.$state = {
    current: '我是第四种修改方式',
    name: '小满娃娃',
    num: 5000,
}
</script>

<style scoped></style>

需要注意的是$state中的属性的key和value的类型必须和仓库中的state一致,属性数量也要相同,否则直接报错

<template>
    <div>pinia:{{ Test.current }}---{{ Test.name }}---{{ Test.num }}</div>
</template>

<script setup lang="ts">
import { useTestStore } from './Store'

const Test = useTestStore() 
// 报错,少了name和num属性
Test.$state = {
    current: '我是第四种修改方式',
}
</script>

<style scoped></style>

第五种:通过派发action进行修改(需要提前在store的action选项中配置好对应方法)

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

export const useTestStore = defineStore(Names.TEST, {
    state: () => {
        return {
            current: '开始你的炸弹秀',
            name: '小满zs',
            num: 6,
        }
    },
    // computed一些值
    getters: {},
    // 相当于methods,可以做同步、异步操作、提交state等
    actions: {
        setCurrent(){
          // 别忘了加上this哦~
            this.current = '我是第五种修改方式'
        }
    },
})
<template>
    <div>pinia:{{ Test.current }}---{{ Test.name }}---{{ Test.num }}</div>
    <button @click="change">派发Action</button>
</template>

<script setup lang="ts">
import { useTestStore } from './Store'

const Test = useTestStore()
const change = () => {
    Test.setCurrent()
}
</script>

<style scoped></style>

同样的,setCurrent函数也可以接收参数

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

export const useTestStore = defineStore(Names.TEST, {
    state: () => {
        return {
            current: '开始你的炸弹秀',
            name: '小满zs',
            num: 6,
        }
    },
    // computed一些值
    getters: {},
    // 相当于methods,可以做同步、异步操作、提交state等
    actions: {
        setCurrent(str:string){
            this.current = str
        }
    },
})
<template>
    <div>pinia:{{ Test.current }}---{{ Test.name }}---{{ Test.num }}</div>
    <button @click="change">派发Action</button>
</template>

<script setup lang="ts">
import { useTestStore } from './Store'

const Test = useTestStore()
const change = () => {
    Test.setCurrent('只要狗洛在,前端一片光明')
}
</script>

<style scoped></style>
相关文章
|
7月前
|
Java 关系型数据库 数据库
微服务——SpringBoot使用归纳——Spring Boot事务配置管理——Spring Boot 事务配置
本文介绍了 Spring Boot 中的事务配置与使用方法。首先需要导入 MySQL 依赖,Spring Boot 会自动注入 `DataSourceTransactionManager`,无需额外配置即可通过 `@Transactional` 注解实现事务管理。接着通过创建一个用户插入功能的示例,展示了如何在 Service 层手动抛出异常以测试事务回滚机制。测试结果表明,数据库中未新增记录,证明事务已成功回滚。此过程简单高效,适合日常开发需求。
944 0
|
10月前
|
人工智能 编解码 虚拟化
See3D:智源研究院开源的无标注视频学习 3D 生成模型
See3D 是智源研究院推出的无标注视频学习 3D 生成模型,能够从大规模无标注的互联网视频中学习 3D 先验,实现从视频中生成 3D 内容。See3D 采用视觉条件技术,支持从文本、单视图和稀疏视图到 3D 的生成,并能进行 3D 编辑与高斯渲染。
289 13
See3D:智源研究院开源的无标注视频学习 3D 生成模型
|
敏捷开发 运维 Devops
IT入门知识第三部分《软件开发》(3/10)
IT入门知识第三部分《软件开发》(3/10)
220 0
|
域名解析 网络协议 安全
服务器部署后 访问出错的原因和解决办法
部署到服务器后无法访问可能是由于配置错误、权限问题或网络设置不当。解决办法包括检查服务器配置文件、确保文件路径正确、调整权限设置以及检查防火墙和端口设置。
1273 0
|
前端开发 JavaScript
vite中css最佳实践:使用postcss完善项目中的css配置
【8月更文挑战第3天】使用postcss完善项目中的css配置
2992 1
|
JavaScript
vue 打印vue-print-nb
vue 打印vue-print-nb
568 0
|
JSON JavaScript 定位技术
Echarts 绘制地图(中国、省市、区县),保姆级教程!
Echarts 绘制地图(中国、省市、区县),保姆级教程!
|
前端开发
element-plus的自动导入和按需导入
element-plus的自动导入和按需导入
1177 0