之前说过,对state的修改必须经过mutations,而mutations中是用来定义方法的,在vue文件中通过提交某个方法来完成state的修改,比如说现在点击一个按钮,让counter+1,规范的做法如下:vue文件:templatedivh1...
之前说过,对state的修改必须经过mutations,而mutations中是用来定义方法的,在vue文件中通过提交某个方法来完成state的修改,比如说现在点击一个按钮,让counter+1,规范的做法如下:
vue文件:
<template>
<div>
<h1>我是首页页面</h1>
<h2>{{$store.state.counter}}</h2>
<button @click="addition">counter+1</button>
</div>
</template>
methods: {
addition(){
this.$store.commit('increment')
}
}
store下的index.js:
const store = new Vuex.Store({
state:{
counter: 1000
},
getters: {},
mutations: {
increment(state,getters){
state.counter++
}
},
actions: {},
modules: {}
})
要调用mutations中的方法,必须通过commit的方式来提交,参数是方法的名称。还有一个注意点的是,mutations中的方法默认参数1是state对象,可拿到state中的变量,参数二是getters,可调用getters中的方法
沃梦达教程
本文标题为:vue-vuex-mutations的基本使用
基础教程推荐
猜你喜欢
- CSS3的几个标签速记(推荐) 2024-04-07
- 浅析canvas元素的html尺寸和css尺寸对元素视觉的影响 2024-04-26
- this[] 指的是什么内容 讨论 2023-11-30
- js禁止页面刷新与后退的方法 2024-01-08
- 浅谈Vue2和Vue3的数据响应 2023-10-08
- JS前端广告拦截实现原理解析 2024-04-22
- 关于文字内容过长,导致文本内容超出html 标签宽度的解决方法之自动换行 2023-10-28
- vue离线环境如何安装脚手架vue-cli 2025-01-19
- 基于Vue制作组织架构树组件 2024-04-08
- Ajax实现动态加载数据 2023-02-01
