深海 深海
首页
  • 随手笔录
  • 电影
  • 音乐
  • 书籍
汇总
面试
  • 开发工具
  • VScode插件
  • Git
  • Mac时代
  • 前端工具
  • Chrome
  • HTML
  • CSS
  • Javascript
  • Typescript
  • Axios
  • 框架

    • Vue
    • uni-app
  • Nginx
  • Linuk
事例
关于

深海

人生如逆旅,我亦是行人
首页
  • 随手笔录
  • 电影
  • 音乐
  • 书籍
汇总
面试
  • 开发工具
  • VScode插件
  • Git
  • Mac时代
  • 前端工具
  • Chrome
  • HTML
  • CSS
  • Javascript
  • Typescript
  • Axios
  • 框架

    • Vue
    • uni-app
  • Nginx
  • Linuk
事例
关于
  • HTML
  • CSS的奥秘

  • JavaScript

  • TypeScript

  • Vue

    • 介绍
    • 基础
    • Vuex
      • Vuex 是什么
      • 使用Vuex
        • 1.安装vuex
        • 2.配置vuex文件
        • 3.挂载到根实例中
        • 4.使用
        • State
        • Actions
        • Mutation
        • Getters
        • Modules
        • 1.模块配置
        • 2.模块使用
    • Vue-Router
    • Vue-CLI
    • Vue3
    • 项目搭建
    • Vue3

  • uni-app

  • 前端
  • Vue
深海
2023-05-25
目录

Vuex

# Vuex 是什么

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式,它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。 Vuex官网 (opens new window)

# 使用Vuex

注意

Vue2中,要用Vuex3版本
Vue3中,要用Vuex4版本

# 1.安装vuex

npm install vuex
1

# 2.配置vuex文件

// 创建store/index.js文件
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
    state:{},
    actions:{},
    mutations:{},
    getters:{}
})
1
2
3
4
5
6
7
8
9
10

# 3.挂载到根实例中

import store from '@/store/index.js'
new Vue({
  render: h => h(App),
  store,
}).$mount('#app')
1
2
3
4
5

# 4.使用

# State

存储数据

import {mapState} from 'vuex';
const state = {
    name:"张三"
}
computed:{
    // 1.直接获取state中的属性
    this.$store.state.name;
    // 2.辅助函数获取,页面直接调用即可
    ...mapState({'myName':'name'}),
    ...mapState(['name']),
}
1
2
3
4
5
6
7
8
9
10
11

# Actions

Action 提交的是 mutation,而不是直接变更状态。Action 可以包含任意异步操作。

import {mapActions} from 'vuex';
const actions = {
    updateName(context,value){
        context.commit('UPDATE_NAME',value)
    }
}
methods:{
    // 1. 直接调用actions中的方法,使用dispatch
    this.$store.dispatch('updateName',name)
    // 2. 辅助函数调用actions
    ...mapActions(['updateName']),
    ...mapActions({'SetUpdateName':'updateName'}),
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# Mutation

用于更改state中的数据

import {mapMutations} from 'vuex';
const mutations = {
    UPDATE_PRICE(state,value){
        state.name = value;
    }
}
methods:{
    // 1. 直接调用mutations中的方法,使用commit
    this.$store.commit('UPDATE_PRICE',price)
    // 2. 辅助函数调用mutations
    ...mapMutations({'SET_UPDATE_PRICE':'UPDATE_PRICE'}),
    ...mapMutations(['UPDATE_PRICE']),
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# Getters

从 store 中的 state 中派生出一些状态,类似于组件中的计算属性

import {mapGetters} from 'vuex';
const getters = {
    completeName (state){
        return '姓名:' + state.name
    },
    completeName:state => '姓名:' + state.name;
}
methods:{
    // 1. 直接获取getters中的属性
    this.$store.getters.completeName;
    // 2. 辅助函数获取getters中的属性
    ...mapGetters({'getName':'completeName'})
    ...mapGetters(['completeName'])
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# Modules

Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter。用于模块化管理

# 1.模块配置
// 订单模块
const orderModule = {
    namespaced:true,  //使其成为带命名空间的模块
    state:{},
    actions:{},
    mutations:{},
    getters:{}
}
// 人员信息模块
const personeModule = {
    namespaced:true,  //使其成为带命名空间的模块
    state:{},
    actions:{},
    mutations:{},
    getters:{}
} 
export default new Vuex.Store({
   modules:{
    a:orderModule,
    b:personeModule
   }
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 2.模块使用
  • 辅助函数
import {mapState,mapMutations,mapGetters,mapActions} from 'vuex'
computed:{
    // 模块间mapState的两种获取  a:A模块  b:B模块
    ...mapState('a',{'getName':'name'}),
    ...mapState('b',['list']),
    // mapGetters中的获取
    ...mapGetters('a',['completeName']),
    ...mapGetters('b',{'completeName':'completeName'})
},
methods:{
    // mapMutations的两次获取方式
    ...mapMutations('a',['UPDATE_PRICE']),
    ...mapMutations('b',{'addPersonInfo' :'ADD_PERSION'}),
    // mapActions的两种获取方式
    ...mapActions('a',['setName']),
    ...mapActions('a',{'setUpdateName' :'setName'}),
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  • 普通调用
// 获取A模块中的name属性
this.$store.state.a.name;
// 提交mutations方法  a模块下的mutations的SET_NAME方法
this.$store.commit('a/SET_NAME',name)
// 提交actions中的方法 b模块下的actions中的addPersonInfo方法
this.$store.dispatch('b/addPersonInfo',data)
// 获取getters a模块下的completeName方法
this.$store.getters['a/completeName']
1
2
3
4
5
6
7
8
上次更新: 2024/10/09, 16:59:47
基础
Vue-Router

← 基础 Vue-Router→

最近更新
01
TypeScript是什么
06-15
02
项目搭建
05-21
03
Prettier
02-27
更多文章>
Theme by Vdoing | Copyright © 2022-2025 京ICP备2020044002号-4 京公网安备11010502056618号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式