vuex - 常用命令学习及用法整理

https://vuex.vuejs.org/zh-cn/api.html

命令

用途

备注

this.$store

组件中访问store实例

 

store.state.a

获取在state对象中的数据

store文件中

$store.state.a

视图上(即标签上)

 

 

this.$store.state.a

1) 组件内部,data数据里边

2) 组件计算属性返回值

computed: { 

   count () { return this.$store.state.count }

 }

 

store.getters

派生state的一些状态(即拿到,再进行一些小变化后返回新的状态,比如给state中某个数组内部小于10的前边加上0再返回)

Getter对象

this.$store.getters.oneFunction

访问Getter对象内的某个属性的值

在组件的计算属性中使用

store.commit()

【方法】提交 mutation,触发状态变更(修改state的值)

action文件中

this.$store.commit(state,payload)

在组件的 methods 中

store.dispatch()

【方法】触发 action,以间接地触发mutation,进而使状态变更(修改state的值)

action文件中

 

this.$store.dispatch('incrementBy',amount)

在组件的 methods 中

'incrementBy':  mutationFunctionName

amount =  payload

 

2018-04-07  18:00:52

posted @ 2018-04-07 17:59  xing.org1^  阅读(741)  评论(0编辑  收藏  举报