关于vuex

复制代码
state:{
  count:0
}
// 原始形式调用时插值表达式形式
<div>{{$store.state.count}}</div>
赋值函数形式是在computed中
important {mapStatte} from'vuex'
computed:{
...mapState['count']
count(){
return:this.$store.state.count
}
}
复制代码
复制代码
1 // 修改state
2 mutations:{
// es5
// addcount:function(){}
// es6
// state指的是当前vuex中的state对象
// payload 载荷提交mutation时传递的参数
3 addCount(state,payload){ 4 state.count += payload 5 } 6 }
// 直接调用形式
this.$store.commit('addCount',10)
// 辅助函数形式 在methods中调用mapMutations
important {} from 'vuex'
methods:{
// addCount(){
// this.$store.commit('addCount')
// }
...mapMatations['addcount'] // 此时方法中就会有一个对应的addCount方法
}
复制代码
复制代码
// action 异步动作 
actions:{
   // action 参数 context 相当于 组件中的this.$store stroe的运行实例
   getAsyncCount(){
// 模拟异步请求
      setTimeout(()=>{
         context.commit('addCount',123)
      },1000)
  }
}

// 原始形式调用action 
// diapatch 调用action
// dispatch(action名称,传递参数)
this.$store.dispatch('getAsyncCount')

// 辅助函数调用
 important mapActions(['getAsynCount'])
methods:{
  ...mapActions(['getAsynCount']) // 相当于在方法中有一个getAsynCount
}
复制代码
复制代码
getters:{
  // 放置所有的vuex的计算属性
  // state 指的就是当前store中的state
// es5
 //   filterList:function(state){
  //     state.list.filter(item =>item>5)
  //  }
// es6 写法
     filterList:state => state.list.filter(item =>item>5)
}
原始调用形似
<div>{{$store.getters.filterList}}</div>
辅助函数形式---mapGetters
important {mapGetters} from 'vuex'
computed:{
  ...mapGetters(['filterList'])
}
复制代码

 

复制代码
// 模块化

 


 读取子模块的state

 

 

 

 


 

// 命名空间 nameSpaced

 


 

 

 

 


 

 

 

 

 

复制代码

 

posted @   Harry宗  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示