未找到 Vuex Store getter 函数

far*_*ahm 1 getter store typescript vue.js vuex

这是我在 vuex 商店中的吸气剂:

  const getters= {
    getUser: (state:any) =>  {return state.user}
  };
Run Code Online (Sandbox Code Playgroud)

在我看来我这样做:

<script lang="ts">
  ...
  import {createNamespacedHelpers} from 'vuex'
  const {mapMutations, mapGetters} = createNamespacedHelpers('user');

  export default Vue.extend({

    computed: {
      ...mapGetters([
        'getUser'
      ])
    },

    methods: {
      ...mapMutations([
        'change'
      ]),
      login() {
        this.change(email); //This is a function in the mutations. And this is working
        this.loggedin = this.getUser(); // Here it says error

      }
    }
  });
Run Code Online (Sandbox Code Playgroud)

我收到错误:

TypeError: this.getUser is not a functionthis.change(email);不过,对突变函数的调用正在工作。

Sat*_*hak 5

computed属性不是methods这样,因此它们不会被调用。它的行为类似于settersgetters

this.getUser是一个计算属性,其行为更像是一个值本身。您试图将其视为method当您放置()后使其调用时。这是不可能的,因为这不是fn出现该错误的原因。

请参阅此 - https://v2.vuejs.org/v2/guide/compulated.html#Compulated-Caching-vs-Methods

Vuex getters/states包含在computedvue 的属性中instances,因为它们的工作本身就是返回value. 但是,mutations/actions包含在methods属性内部,因为它的工作是perform操作示例 - 发出async请求并更新state