网站首页 > 技术文章 正文
1 安装vuex
npm install vuex -S
2 导入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的使用流程)
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)