• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

joer717

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

vuex中mapGetters,mapActions

1.首先在store中定义getter,导出

var getters={
 //需要传个形参,用来获取 state 属性
 count(state){
  return state.count
 }
}
// 创建 store 对象
const store = new Vuex.Store({
 state,
 getters
})

2.在使用页面中引用vuex

import {mapGetters} from 'vuex'

然后在计算属性中调用 mapGetters 辅助方法,并传入一个数组,在数组中指定要获取的属性  count

<script>
import {mapGetters,mapActions} from 'vuex'
export default {
 name: 'app',
 computed:mapGetters([
 //此处的 count 与以下 store.js 文件中 getters 内的 count 相对应
 'count'
 ])
}
</script>
//然后就this.count 就可以使用了

 

 

 

下面说说mapActions 

1.编写actions mutations

2.导出anction mutations

3.在页面中引入 mapactions

4.在方法中mapactions这个方法

methods:mapActions([
 //该 increment 来自 store.js 中导出的 actions 和 mutations 中的 increment 
 'increment',
 ])

 

posted on 2019-05-16 11:02  joer717  阅读(124)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3