[Nuxt] Update Vuex State with Mutations and MapMutations in Vue.js
2023-09-14 08:59:18 时间
You commit changes to state in Vuex using defined mutations
. You can easily access these state mutations in your template using mapMutations
. This lesson shows you have to wire together your Vue.js template with your Vuex store using mutations and mapMutations.
store/index.js:
import Vuex from 'vuex' const store = () => new Vuex.Store({ state: { counter: 0 }, mutations: { increment: (state) => { state.counter++ }, decrement: (state) => { state.counter-- } } }) export default store
pages/index.vue:
<template> <div> Counter: {{counter}} <button @click='increment'>+</button> <button @click='decrement'>-</button> </div> </template> <script> import { mapState, mapMutations } from 'vuex' export default { computed: { ...mapState({ counter: (state) => state.counter }) }, methods: { ...mapMutations([ 'increment', 'decrement' ]) } } </script>
相关文章
- Vue中显示img图片,显示不出来怎么办?vue显示图片
- 浅谈Vue.js_Vue js quote
- Vue笔记(10) vue-router
- JS跳转代码_js中跳转页面路径
- VUE 路由切换白屏的问题
- Vue学习笔记之vue.js 两个等号 == 和三个等号===的区别 数字0和空字符串
- vue双向绑定原理面试题_面试可以记笔记吗
- js中四舍五入的方法_JS取整
- js读取本地json_vue读取本地json文件
- vue富文本编辑器tinymce_vue移动端富文本编辑器
- vue.js客服系统实时聊天项目开发(十二)自动欢迎信息的延迟展示效果
- 开心档-软件开发入门之Vue.js Ajax(vue-resource)
- 解决Vue中”This dependency was not found”的方法详解编程语言
- JS技术连接Oracle数据库实现数据交互(js连接oracle实例)
- 基于Vue的Redis网页设计(vue设计redis页面)
- Vue用Redis储存获取数据(vue获取redis)
- js获取当前地址JS获取当前URL的示例代码