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>
相关文章
|
存储 前端开发 索引
【面试题】数组去重的五种方法(必会)
【面试题】数组去重的五种方法(必会)
768 1
|
存储 开发框架 JavaScript
在Vue3项目中使用pinia代替Vuex进行数据存储
在Vue3项目中使用pinia代替Vuex进行数据存储
|
移动开发 JavaScript 前端开发
HTML5 MathML好用的第三方库推荐
HTML5 的 MathML 对数学公式的展现至关重要,但因浏览器兼容性和复杂性问题,开发者常选用第三方库增强其功能。本文推荐了四个库:MathJax、KaTeX、MathML Cloud 和 jsMath。MathJax 兼容性好,支持多种格式;KaTeX 渲染速度快,适合现代浏览器;MathML Cloud 提供云端转换服务;jsMath 则适用于基本 MathML 支持。根据项目需求选择合适的库,能显著提升数学内容展示质量和用户体验。
|
移动开发 前端开发 JavaScript
使用html-to-image代替html2canvas,结合jspdf实现下载pdf(下载截图下载前端dom元素)
本文介绍了在前端项目中,当使用`html2canvas`遇到问题时,如何使用`html-to-image`库作为替代方案,结合`jspdf`实现将DOM元素生成为PDF文件并提供下载。文章首先讨论了`html2canvas`可能遇到的问题,并提供了该库的使用示例代码。随后,详细介绍了`html-to-image`库的安装和使用方法,展示了如何将DOM元素转换为Canvas,再利用`jspdf`生成PDF文件。最后,文章通过示例代码说明了整个转换和下载的过程,并展示了效果截图。
2067 0
|
移动开发 前端开发
vue3 Element-Plus封装的el-tree-select的使用
vue3 Element-Plus封装的el-tree-select的使用
3816 0
vue3 Element-Plus封装的el-tree-select的使用
|
存储 缓存 网络协议
使用外链图片的时候报 403 怎么处理?
使用外链图片的时候报 403 怎么处理?
736 0
使用外链图片的时候报 403 怎么处理?
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16783 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置