美文网首页
vuex getter and actions

vuex getter and actions

作者: strong9527 | 来源:发表于2016-08-16 22:11 被阅读2528次

vuex 中的 getters

文档对于component 中 vuex 属性块的描述其中包括了对 getter 的描述(黑体标粗).

Note the special vuex option block. This is where we specify what state the component will be using from the store. For each property name, we specify a getter function which receives the entire state tree as the only argument, and then selects and returns a part of the state, or a computed value derived from the state. The returned result will be set on the component using the property name, just like a computed property.

getters函数必须是纯净的.

All Vuex getters must be pure functions - they take the entire state tree in, and return some value solely based on that state. This makes them more testable, composable and efficient. It also means you cannot rely on this inside getters.

对应代码


//  vuex/store.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const state = {
  count: 0
}

const mutations = {
  INCREMENT (state) {
    state.count++
  }
}

export default new Vuex.Store({
  state,
  mutations
})


// templages/Display.vue

<template>
  <div>
    <h3>Count is {{ counterValue }}</h3> 
    //没有传任何值,而且没有调用,就可直接用呀
  </div>
</template>

<script>
import { getCount } from '../vuex/getters'
export default {
  vuex: {
    getters: {
            counterValue: getCount
    }
  }
}
</script>


//vuex/getter.js
export function getCount (state){
    return state.count;
}


Actions

Actions are just functions that dispatch mutations. By convention, Vuex actions always expect a store instance as its first argument, followed by optional additional arguments:



//   vuex/action.js

export const incrementCounter = function ({ dispatch, state }) {
  dispatch('INCREMENT', 1)
}



// components/Increment.vue

<template>
  <div>
    <button @click='increment'> Increment +1 </button>
  </div>
</template>

<script>
import { incrementCounter } from '../vuex/action'
export default {
  vuex: {
    actions: {
      increment: incrementCounter
    }
  }
}
</script>

getter 和 actions 给我个人的感觉就像java 类中的 get 和 set 函数

未完待续vue学习中,今天刚看了一下vuex弥补一下没有学习flux的遗憾,准备利用
vue,vuex,node.js重构一下微博应用,如果时间充裕再用vue写一个消消乐游戏

相关文章

  • vuex getter and actions

    vuex 中的 getters 文档对于component 中 vuex 属性块的描述其中包括了对 getter ...

  • vue2视频教程系列第二十七节—vuex中getters和act

    这节课主要跟大家一起学习vuex中getters和actions的使用 Getter和action在vuex里不经...

  • Vue 新的状态管理 Pinia

    Pinia 和 Vuex Vuex:State、Gettes、Mutations(同步)、Actions(异步) ...

  • Vue3+TS Day25 - vuex、actions、mod

    一、actions 1、actions和mutation都是用于修改vuex数据,有什么不同? actions提交...

  • getter

    getter vuex 的 '计算属性'

  • 手写Vuex源码

    Vuex源码实现 1. Vuex核心概念State,Getters,Mutations, Actions, Mod...

  • Vuex

    getter mutation and action // actions // mutation是同步操作,如果...

  • vuex Getter

    我们需要从store中的state中派生出一些状态,例如对列表进行过滤并计数: 多个组件需要用到此属性,我们要么复...

  • 前端面试笔记

    1.vuex:store{state, mutations, actions, getters,modules};...

  • 再读Vuex

    vuex是vue的状态管理工具 在components中通过dispatch来触发actions,actions通...

网友评论

      本文标题:vuex getter and actions

      本文链接:https://www.haomeiwen.com/subject/zcapsttx.html