从零开始学VUE之VueX(modules)
modules
import Vue from 'vue' // 导入vuex import Vuex from 'vuex' import {INCR} from "./type"; // 通过vue安装vuex Vue.use(Vuex) /** * 创建store * @type {Store<{counter: number}>} */ const store = new Vuex.Store({ // 用于定义属性 state:{ counter:1000 }, // 定义用于修改属性的函数 同步提交 mutations:{ [INCR](state){ state.counter+=100; }, // 第一个参数是 state modifyCounter(state){ state.counter--; }, // 传递参数 modifyCounterVal(state,val){ state.counter += val; } }, // 计算属性也就是getters 用于获取 getters:{ // 获取平方 getCountPF(state) { return state.counter * state.counter; }, // 获取 平方的2分之一 getCountTwoOne(state, getters) { return getters.getCountPF / 2; }, // 获取 平方的n分之一 参数传递 getCountN(state,getters){ return function (n){ return getters.getCountPF / n; } } }, // 用于处理异步状态修改 actions:{ updateInfo(context,playod){ // console.log(playod) // // 模拟网络请求 // setTimeout(() => { // // 传参 // console.log(playod.message); // // action 调用 mutations 修改 // context.commit(INCR); // // 回调 // playod.success(); // },1000) /** * 返回 Promise,让外面可以通过then捕获返回结果 */ return new Promise((resolve,reject) => { // 模拟网络请求 setTimeout(() => { // 传参 console.log(playod.message); // action 调用 mutations 修改 context.commit(INCR); // 回调 resolve("ajax return data!") },1000) }) } }, // 用于划分不同模块的状态的 里面可以写 state getters... modules:{ a:{ // 通过$store.state.a.name 调用 state:{ name:"a_modules" }, // 通过$store.commit() 调用 方法名不要和外面的冲突 mutations:{ // 自己的state playod:参数 getConsole(state,playod){ return console; } }, // 通过$store.getters.方法调用 方法名不要冲突 getters:{ // state:自己的 getters:自己的 rootState:外面的state getComputed(state,getters,rootState){ state.name = "1111"; } }, // 通过$store.dispatch()调用 actions:{ // context:上下文对象 只能调用自己的mutations方法 // 如果想获取外面的可以使用 context.rootGetters/... updateName2(context){ } } } } }) export default store import Vue from 'vue' // 导入vuex import Vuex from 'vuex' import {INCR} from "./type"; // 通过vue安装vuex Vue.use(Vuex) /** * 创建store * @type {Store<{counter: number}>} */ const store = new Vuex.Store({ // 用于定义属性 state:{ counter:1000 }, // 定义用于修改属性的函数 同步提交 mutations:{ [INCR](state){ state.counter+=100; }, // 第一个参数是 state modifyCounter(state){ state.counter--; }, // 传递参数 modifyCounterVal(state,val){ state.counter += val; } }, // 计算属性也就是getters 用于获取 getters:{ // 获取平方 getCountPF(state) { return state.counter * state.counter; }, // 获取 平方的2分之一 getCountTwoOne(state, getters) { return getters.getCountPF / 2; }, // 获取 平方的n分之一 参数传递 getCountN(state,getters){ return function (n){ return getters.getCountPF / n; } } }, // 用于处理异步状态修改 actions:{ updateInfo(context,playod){ // console.log(playod) // // 模拟网络请求 // setTimeout(() => { // // 传参 // console.log(playod.message); // // action 调用 mutations 修改 // context.commit(INCR); // // 回调 // playod.success(); // },1000) /** * 返回 Promise,让外面可以通过then捕获返回结果 */ return new Promise((resolve,reject) => { // 模拟网络请求 setTimeout(() => { // 传参 console.log(playod.message); // action 调用 mutations 修改 context.commit(INCR); // 回调 resolve("ajax return data!") },1000) }) } }, // 用于划分不同模块的状态的 里面可以写 state getters... modules:{ a:{ state:{ name:"a_modules" } } } }) export default stor
app.vue
属性访问
<h2>访问store modules</h2>
<h3>{{ $store.state.a.name }}</h3>
作者:彼岸舞
时间:2021\06\28
内容关于:VUE
本文属于作者原创,未经允许,禁止转发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」