vuex中的五大家族😳😳

117 阅读1分钟

有一种缘分叫遇见

五分之一state 作用:保存公共数据

语法

{
state(){
return{
数据1:值1
 }
   }
    }
   使用:this.$store.state.数据1

五分之二 mutations 作用:修改数据

语法

{
mutations:{
方法名(state,参数){
}
}
}
使用:this.$store.commit("方法名",实参)

五分之三 getters 作用:计算属性

语法

{
getters:{
方法名(state){
return 计算结果
}
}
}
使用:this.$store.getters.方法名

五分致死 actions 作用;发送异步请求

语法

 {   
 actions: {      
 方法名(context, 参数){  
 axios.....
 context.commit('mutations方法名')
 }   }}
 使用:  this.$store.dispatch('方法名',实参)

modules 作用:模块拆分

定义

  store   
  index.js  // 引入模块1.js      
  modules  
  模块1.js

使用

 import 模块1 from'./modules/模块1.js'modules: {     
 模块1   
 }}

模块一

export default {   
namespaced: true,   
state,   
mutations,  
getters,    
actions
}

调用 image.png