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

vuex的使用笔记

1.安装

npm安装

npm install vuex@next --save

yarn安装

yarn add vuex@next --save

2.基本结构

import Vuex from 'vuex'const store = createStore({
//状态:相当于vue中的data()
state() {return {name: 0,code:"",todos: [{ id: 1, text: '...', done: true },{ id: 2, text: '...', done: false }]}},getters: {doneTodos (state) {return state.todos.filter(todo => todo.done)}},//更改 Vuex 的 store 中的状态的唯一方法是提交 mutation//mutation 必须同步执行mutations: {saveName(state,name){state.name= name;},saveCode(state,code){state.code= code;},},//Action 类似于 mutation//Action 提交的是 mutation,而不是直接变更状态//Action 可以包含任意异步操作actions: {//Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象//Action 通过 store.dispatch 方法触发increment (context) {context.commit('increment')}},
})export default store 

3.多模块的结构

import Vuex from 'vuex'
const module1 = {
//开启命名空间namespaced: true,state: () => ({ ... }),mutations: { ... },actions: { ... },getters: { ... }
}
const module2 = {
//开启命名空间namespaced: true,state: () => ({ ... }),mutations: { ... },actions: { ... },getters: { ... }
}
const module3 = {
//开启命名空间namespaced: true,state: () => ({ ... }),mutations: { ... },actions: { ... },getters: { ... }
}
const store = createStore({
modules: {module1: module1,module2: module2,module3: module3,}
})
export default store 

main.js 注册store


// 引入仓库
import store from 'path';new Vue({el: '#app',store,render: h => h(App)
});

4.使用方法

1.获取状态

//方法1
this.$store.state.状态名
//方法2 多模块的状态名
this.$store.state.模块名.状态名
//方法3 单模块映射的方式,在vue中的计算属性中混入
computed:{
...mapState({'状态名1','状态名2',...
})
}
//使用‘this.状态名即可获取’
//多模块映射的方式
computed:{
...mapState({module1: state => state.some.nested.module.module1,module2: state => state.some.nested.module.module2})
}

2.调用getter

//直接使用
this.$store.getters.doneTodosCount
//映射的方式
computed: {// 使用对象展开运算符将 getter 混入 computed 对象中...mapGetters(['doneTodosCount','anotherGetter',// ...])}

3. 调用mutations

//方法1 使用this.$store
//1.b不带参数
this.$store.commit('mutations的方法名')
//2.带参数
this.$store.commit('mutations的方法名',parmas)
//3.多模块的调用方式
this.$store.commit('模块名/调用模块的mutations的方法名',parmas)
//4.映射的方式 在methods中使用
methods: {...mapMutations(['increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`// `mapMutations` 也支持载荷:'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`]),}

4.调用Action

//通过this.$store.dispatch 方法触发
//映射的方式 在methods中使用import { mapActions } from 'vuex'methods: {...mapActions(['increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`// `mapActions` 也支持载荷:'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`]),...mapActions({add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`})}
http://www.lryc.cn/news/245956.html

相关文章:

  • 汇编:关于栈的知识
  • uniapp使用map标签
  • MacOS14 Sonoma 安装 Flutter 开发环境
  • 【Web】PHP反序列化刷题记录
  • C++标准模板库 STL 简介(standard template library)
  • Linux篇:文件系统
  • AI - Crowd Simulation(集群模拟)
  • <JavaEE> Java中线程有多少种状态(State)?状态之间的关系有什么关系?
  • 正则表达式 通配符 awk文本处理工具
  • 三、ts高级笔记,
  • 二十一、数组(6)
  • flask依据现有的库表快速生成flask实体类
  • .NET6 开发一个检查某些状态持续多长时间的类
  • 链表K个节点的组内逆序调整问题
  • 安卓隐私指示器学习笔记
  • 【Jenkins】jenkins发送邮件报错:Not sent to the following valid addresses:
  • CSS3制作3D爱心动画
  • Python Opencv实践 - 全景图片拼接stitcher
  • echarts 几千条分钟级别在小时级别图标上展示
  • 操作系统的中断与异常(408常考点)
  • linux下的工具---vim
  • 代码随想录算法训练营第六十天|84. 柱状图中最大的矩形
  • P14 C++局部静态变量static延长生命周期
  • C语言:写一个函数,求字符串的长度,在main函数中输入字符串并输出其长度(指针)
  • CentOS7安装Docker运行环境
  • 单片机调试技巧--栈回溯
  • 分布式锁之基于redis实现分布式锁(二)
  • python中%s的用法(字符串变量赋值办法),长字符串换行办法
  • 【Mybatis】预编译/即时sql 数据库连接池
  • 物联网AI 无线连接学习之WiFi基础篇 802.11协议发展