当前位置: 首页 > news >正文

vuex 和持久化 vuex-persistedstate

1.store文件下index.js

import { createStore } from 'vuex'
import api from '../api/index.js'
import axios from "../utils/request.js";
import path from "../api/path.js";
import cityModule from './cityModule.js';export default createStore({state: {counter: 10},getters: {//获取方法getCounter(state) {return state.counter > 0 ? state.counter : 'counter小于数据等于0';}},mutations: {//修改方法addCounter(state, num) {state.counter = num;}},actions: {//actions 可以异步操作   同步操作用mutationsasycnAddCounter({ commit }, num) {console.log('actions:内方法获取到参数:', num);axios.post(path.gen_list, { user: "iii" }).then(res => {commit("addCounter", res.data[0])})}},modules: {cityModule,}
})

2.cityModule.js

const cityModule = {namespaced: true,//分模块命名//子模块state建议写成函数state: () => {return {cityName: '石家庄'}},mutations: {changeCity(state, name) {state.cityName = name;}},actions: {changeCityAsync({ commit }, name) {setTimeout(() => {commit("changeCity", name)}, 200)}},getters: {// getters  是属性不是方法getCity(state) {return state.cityName+'欢迎您';}}}
export default cityModule

3.hellow.vue

<template><div class="home"><img alt="Vue logo" src="../assets/logo.png"><h3>Home</h3><p>counter={{ $store.getters.getCounter }}</p><!-- 快捷读取方案 --><!-- <p>counter={{ getCounter }}</p> --><button @click="add">vuex mutations 增加counter值</button><button @click="asyncClick">vuex aciton 异步修改数据</button><p>city={{ $store.state.cityModule.cityName }}</p><p>city={{ $store.getters["cityModule/getCity"] }}</p><p><button @click="updataCity">修改城市名称</button></p><p><button @click="AsyncUpdataCity">修改城市名称</button></p><HelloWorld msg="Welcome to Your Vue.js App" /></div>
</template><script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'import { mapGetters, mapMutations, mapActions } from 'vuex'  //1.引用import { ref } from 'vue'
import { useStore } from 'vuex'export default {name: 'HomeView',// vue3setup(props) {const store = useStore();console.log('vue3-state', store.state.counter)// console.log('vue3-getters', store.getters.getCounter())const add = () => {store.commit("addCounter", 20);}const asyncClick = () => {store.dispatch("asycnAddCounter", 110);}const updataCity = () => {store.commit("cityModule/changeCity", "正定")}const AsyncUpdataCity = () => {store.dispatch("cityModule/changeCityAsync", "Async正定")}return { add, asyncClick, updataCity, AsyncUpdataCity }},//vue2 写法// components: {//   HelloWorld// },// computed: {//2.获取值//   ...mapGetters(["getCounter"])// },// methods: {//   ...mapMutations(["addCounter"]),//2.修改至//   ...mapActions(["asycnAddCounter"]),//   add() {//     // this.$store.commit("addCounter",15);//固定调取方法方式//     this.addCounter(25);//...mapMutations(["addCounter"]), 快速读取//   },//   asyncClick() {//     // this.$store.dispatch("asycnAddCounter");//固定调取方法方式//     this.asycnAddCounter();//   }// }
}
</script>

4.cnpm install vuex-persistedstate -S 首先安装
然后修改 我们的index.js
注意数据存储必须得触发之后才会存储

import { createStore } from 'vuex'
import api from '../api/index.js'
import axios from "../utils/request.js";
import path from "../api/path.js";
import cityModule from './cityModule.js';import createPersistedstate from 'vuex-persistedstate'//1.首先引入export default createStore({//2.pluginsplugins: [createPersistedstate({key:'info',paths:['cityModule']//需要持久化 哪个模块})],state: {counter: 10},getters: {//获取方法getCounter(state) {return state.counter > 0 ? state.counter : 'counter小于数据等于0';}},mutations: {//修改方法addCounter(state, num) {state.counter = num;}},actions: {//actions 可以异步操作   同步操作用mutationsasycnAddCounter({ commit }, num) {console.log('actions:内方法获取到参数:', num);axios.post(path.gen_list, { user: "iii" }).then(res => {commit("addCounter", res.data[0])})}},modules: {cityModule,}
})
最后编辑于:2025-06-15 10:00:35


喜欢的朋友记得点赞、收藏、关注哦!!!

http://www.dtcms.com/a/268263.html

相关文章:

  • macOS虚拟机登录AppleID,全版本,成功率高
  • 深度学习篇---简单果实分类网络
  • 群晖 DS3617xs DSM 6.1.7 解决 PhotoStation 安装失败问题 PHP7.0
  • 企业智脑:智能营销新纪元——自动化品牌建设与智能化营销的技术革命
  • Unreal Engine 5中的AI知识
  • Yocto项目:嵌入式Linux开发的“万能烹饪手册”
  • 力扣 hot100 Day36
  • C++学习之STL学习:list的模拟实现
  • 【CSS-16】深入理解CSS Transform:从基础到高级应用
  • 条件渲染 v-show与v-if
  • 《自然》发布机器人技术路线图
  • 铸造软件交付的“自动驾驶”系统——AI大模型如何引爆DevOps革命
  • 分布式压测
  • Linux驱动学习day18(I2C设备ap3216c驱动编写)
  • Mybatis----留言板
  • python实战项目81:ZeoDB多线程数据爬取程序(最新稳定好用)
  • Node中Unexpected end of form 错误
  • 【大模型入门】访问GPT_API实战案例
  • 从LLM和MCP的协同过程看如何做优化
  • webUI平替应用,安装简单,功能齐全
  • 基于Java+springboot 的车险理赔信息管理系统
  • 基于udev规则固定相机名称
  • 计算机网络:(七)网络层(上)网络层中重要的概念与网际协议 IP
  • 深度学习图像分类数据集—濒危动物识别分类
  • 如何将 Java 项目打包为可执行 JAR 文件
  • Git使用教程
  • 软考(软件设计师)进程管理—进程基本概念,信号量与PV操作
  • centos7.9安装ffmpeg6.1和NASM、Yasm、x264、x265、fdk-aac、lame、opus解码器
  • 1.8 提示词优化
  • Tuning Language Models by Proxy