网站首页 > 技术文章 正文
1 安装vuex
npm install vuex -S2 导入vuex
import Vuex from "vuex"
Vue.use(Vuex)创建store对象
const store = new Vuex.Stroe({
	state : {
		count : 0
	}
});4将store对象挂载到vue实例中
new Vue({
	el : '#app'
	render: h => h(app),
	router,
	store
})主要核心概念
- State
 - Mutation
 - Action
 - Getter
 
State
State提供唯一的公共数据源, 所有共享的数据都要统一放到Store的State中存储
const store = new Vuex.Stroe({
	state : {count : 0}
});组件中访问State中数据的第一种方式
this.$store.state.全局数据名称组件中访问State中数据的第二种方式
// 从vuex中导入mapState函数
import {mapState} from 'vuex通过导入mapState函数, 将当前组件需要的全局数据 映射为当前组件的computed计算属性
computed: {
	...mapState(['count'])
}Mutaion
mutaion用于变更store中的数据
- 只能通过mutation变更Store中的数据, 不可以直接操作Store中的数据
 - 通过这种方式虽然操作起来繁琐一些,但是可以集中监控所有数据的变化
 
// 定义mutation
const store = new Vuex.Store({
	state : {count: 0},
	mutations: {
		add(state){
			state.count++
		}
	}
})
//触发mutation
methods: {
  handler(){
    this.$store.commit('add')
  }
}commit的作用就是调用某个mutation
mutatitons传递参数
mutations:{
	//定义
	addN(state, step){
		state.count += step
	}
}
//触发mutation
methods: {
  handler(){
    //触发时携带参数
    this.$store.commit('addN', 3)
  }
}触发mutations的第二种方式
//从vuex中按需导入mapMutations函数
import {mapMutations} from 'vuex'通过导入的mapMutations函数,将需要的函数映射为当前组件的methods
methods: {
	...mapMutations(['add', 'addN'])
}
//调用
this.addN(3)Action
Action用于处理异步任务
如果通过异步操作变更数据,必须通过Action,而不能使用mutation,但是在Action中还是通过触发mutaion的方式间接变更数据
const store = new Vuex.Store({
  // 只有mutations中定义的函数才能修改state的数据
	mutations : {
		add(state){
			state.count ++;
		}
	},
	actions:{
		addAsync(context){
      // 在action中不能直接修改sate中的数据,必须通过context.commit()触发mutation
			setTimeout(() => {
				context.commit('add')
			}, 1000);
		}
	}
})
//触发action
methods: {
  handler(): {
    this.$store.dispatch('addAsync')
  }
}触发actions携带参数
const store = new Vuex.Store({
	state: {count: 0},
	mutations: {
		addN(state, step){
			state.count += step
		}
	},
	actions:{
		addNAsync(context, step){
			setTimeout(() => {
				context.commit('addN', step)
			}, 1000)
		}
	}
})
//触发action
methods: {
	handle(){
		this.$store.dispatch('addNAsync', 5)
	}
}Getter
getter 用于对store中的数据进行加工处理形成新的数据
- getter可以对store中已有的数据进行加工处理形成行的数据, 类似Vue的计算属性
 - store中的数据发生变化 getter中的数据也会发生相应的变化
 
//定义getter
const store = new Vuex.Store({
	state : {count: 0},
	getters: {
		showNUm(state){
			return '当前最新的数据是' +state.count;
		}
	}
});使用Getter的方式 一
this.$store.getters.showNUm第二种方式
import {mapGetters} from 'vuex'
computed: {
	...mapGetters(['showNum'])
}
    
猜你喜欢
- 2024-10-02 Vue3,Vuex,集中式状态(数据)管理,四个模块:state、getter
 - 2024-10-02 Vue核心知识:8.4 如何在组件中去使用vuex的值和方法?
 - 2024-10-02 Netflix 开源危机管理工具 Dispatch,真香
 - 2024-10-02 vue组件间传递参数的几种方式(vue组件之间的参数传递)
 - 2024-10-02 vue前端实现与安卓和ios之间的通信
 - 2024-10-02 2024前端面试题(三)(2021前端最新面试题)
 - 2024-10-02 Vue.js应用的四种AJAX请求数据模式以及优缺点
 - 2024-10-02 总结4个方面优化Vue项目(如何优化vue项目)
 - 2024-10-02 Node + Express + Mysql + Vue 全栈开发项目:Todo List
 - 2024-10-02 Vuex使用指南-Actions(三)(vuex的使用流程)
 
- 最近发表
 - 
- 聊一下 gRPC 的 C++ 异步编程_grpc 异步流模式
 - [原创首发]安全日志管理中心实战(3)——开源NIDS之suricata部署
 - 超详细手把手搭建在ubuntu系统的FFmpeg环境
 - Nginx运维之路(Docker多段构建新版本并增加第三方模
 - 92.1K小星星,一款开源免费的远程桌面,让你告别付费远程控制!
 - Go 人脸识别教程_piwigo人脸识别
 - 安卓手机安装Termux——搭建移动服务器
 - ubuntu 安装开发环境(c/c++ 15)_ubuntu安装c++编译器
 - Rust开发环境搭建指南:从安装到镜像配置的零坑实践
 - Windows系统安装VirtualBox构造本地Linux开发环境
 
 
- 标签列表
 - 
- cmd/c (90)
 - c++中::是什么意思 (84)
 - 标签用于 (71)
 - 主键只能有一个吗 (77)
 - c#console.writeline不显示 (95)
 - pythoncase语句 (88)
 - es6includes (74)
 - sqlset (76)
 - apt-getinstall-y (100)
 - node_modules怎么生成 (87)
 - chromepost (71)
 - flexdirection (73)
 - c++int转char (80)
 - mysqlany_value (79)
 - static函数和普通函数 (84)
 - el-date-picker开始日期早于结束日期 (76)
 - js判断是否是json字符串 (75)
 - c语言min函数头文件 (77)
 - asynccallback (87)
 - localstorage.removeitem (77)
 - vector线程安全吗 (73)
 - java (73)
 - js数组插入 (83)
 - mac安装java (72)
 - 无效的列索引 (74)
 
 
