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

Vuex3学习笔记

文章目录

  • 1,入门案例
    • 辅助函数
  • 2,mutations传参
    • 辅助函数
  • 3,actions
    • 辅助函数
  • 4,getters
    • 辅助函数
  • 5,模块拆分
  • 6,访问子模块的state
    • 辅助函数
  • 7,访问子模块的getters
    • 辅助函数
  • 8,访问子模块的mutations
    • 辅助函数
  • 9,访问子模块的actions
    • 辅助函数

1,入门案例

安装库。

npm install vuex@3

新建store.js。

import Vue from 'vue'
import Vuex from 'vuex'Vue.use(Vuex)const store = new Vuex.Store({state: {count: 0},mutations: {increment(state) {state.count++}}
})export default store

main.js。

new Vue({render: h => h(App),store
}).$mount('#app')

最后,计数器案例。

<template><h1 @click="add">{{ $store.state.count }}</h1>
</template><script>
export default {name: 'App',methods: {add() {this.$store.commit('increment')}}
}
</script>

效果:

在这里插入图片描述

辅助函数

可以将数据自动变成计算属性。

<template><h1 @click="add">{{ count }}</h1>
</template><script>
import { mapState } from "vuex";
export default {name: 'App',methods: {add() {this.$store.commit('increment')}},computed: {...mapState(['count'])}
}
</script>

2,mutations传参

跟着后面写就行,只能传一个参数。

const store = new Vuex.Store({state: {count: 0},mutations: {increment(state, v) {state.count += v}}
})this.$store.commit('increment', 5)

辅助函数

可以将mutations自动变成方法。

<template><h1 @click="increment(5)">{{ count }}</h1>
</template><script>
import { mapState, mapMutations } from "vuex";
export default {name: 'App',methods: {...mapMutations(['increment'])},computed: {...mapState(['count'])}
}
</script>

3,actions

异步操作数据。

const store = new Vuex.Store({state: {count: 0},mutations: {increment(state, v) {state.count += v}},actions: {incrementAction(context, v) {setTimeout(() => {context.commit('increment', v)}, 1000)}}
})this.$store.dispatch('incrementAction', 5)

辅助函数

可以将actions自动变成方法。

<template><h1 @click="incrementAction(5)">{{ count }}</h1>
</template><script>
import { mapState, mapMutations, mapActions } from "vuex";
export default {name: 'App',methods: {...mapActions(['incrementAction']),...mapMutations(['increment'])},computed: {...mapState(['count'])}
}
</script>

4,getters

派生状态,类似于计算属性。

const store = new Vuex.Store({state: {count: 0},mutations: {increment(state, v) {state.count += v}},actions: {incrementAction(context, v) {setTimeout(() => {context.commit('increment', v)}, 1000)}},getters: {count1(state) {return state.count + "个"}}
})$store.getters.count1 

辅助函数

可以将getters自动变成计算属性。

<template><h1 @click="incrementAction(5)">{{ count1 }}</h1>
</template><script>
import { mapState, mapMutations, mapActions, mapGetters } from "vuex";
export default {name: 'App',methods: {...mapActions(['incrementAction']),...mapMutations(['increment'])},computed: {...mapState(['count']),...mapGetters(['count1'])}
}
</script>

5,模块拆分

新建a.js。

新增了一个配置项namespaced。

export default {namespaced: true,state: {count: 0},mutations: {increment(state, v) {state.count += v}},actions: {incrementAction(context, v) {setTimeout(() => {context.commit('increment', v)}, 1000)}},getters: {count1(state) {return state.count + "个"}}
}

b.js。

export default {namespaced: true,state: {count: 0},mutations: {increment(state, v) {state.count += v}},actions: {incrementAction(context, v) {setTimeout(() => {context.commit('increment', v)}, 1000)}},getters: {count1(state) {return state.count + "个"}}
}

改写store.js。

const store = new Vuex.Store({modules: {a, b}
})

6,访问子模块的state

$store.state.a.count
$store.state.b.count

辅助函数

<template><div><h1>{{ count }}</h1></div>
</template><script>
import { mapState } from "vuex";
export default {computed: {...mapState('a', ['count'])}
}
</script>

7,访问子模块的getters

$store.getters['a/count1']
$store.getters['b/count1']

辅助函数

用法与前面一致。

8,访问子模块的mutations

this.$store.commit('a/increment', 1)

辅助函数

用法与前面一致。

9,访问子模块的actions

this.$store.dispatch('a/incrementAction', 1)

辅助函数

用法与前面一致。

http://www.lryc.cn/news/367571.html

相关文章:

  • harbor1.7.1的访问报错502 bad gateway
  • 【C++ STL】模拟实现 string
  • js 选择一个音频文件,绘制音频的波形,从右向左逐渐前进。
  • 灵动岛动效:打造沉浸式用户体验
  • VSCode数据库插件
  • 正点原子[第二期]Linux之ARM(MX6U)裸机篇学习笔记-25 多点电容触摸屏实验
  • B3726 [语言月赛202303] String Problem P
  • htb-linux-3-shocker
  • Elasticsearch - No mapping found for [field_name] in order to sort on
  • Lua 元表(Metatable)深入解析
  • MySQL Show命令集
  • 倩女幽魂搬砖攻略:云手机自动托管搬砖刷本选哪家云手机?
  • php7.3安装phalcon扩展
  • IIoT(智能物联网)的现状、应用及安全
  • YOLOv8_obb的训练、验证、预测及导出[旋转目标检测实践篇]
  • C语言实战:贪吃蛇(万字详解)
  • 定时器更新界面,线程报错
  • 未来AI大模型的发展趋势
  • 【JavaScript函数详解】Day04
  • json和axion结合
  • v1.2.70-FastJson的AutoType机制研究
  • 老旧机子装linux——Xubuntu
  • 关于Redis中事务
  • 【数据分享】《中国文化文物与旅游统计年鉴》2022
  • 设计模式及其在软件开发中的应用
  • LeetCode72编辑距离
  • 竞拍商城系统源码后端PHP+前端UNIAPP
  • 千益畅行,共享旅游卡,灵活同行,畅游无忧的全方位解析
  • Web IDE 在线编辑器综合实践(Web IDE 技术探索 三)
  • Less is more VS 精一 [生活感悟]