未找到Vuex商店getter函数

问题描述 投票:0回答:1

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

  const getters= {
    getUser: (state:any) =>  {return state.user}
  };

我认为我愿意:

<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

      }
    }
  });

我收到错误:

TypeError: this.getUser is not a function。不过,对变异函数的调用this.change(email);正在运行。

typescript vue.js vuex store getter
1个回答
0
投票

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

this.getUser是一个计算属性,其行为更多地类似于一个值本身。当您调用method之后,尝试将其视为()。这是不可能的,因为它不是fn,因此会出现该错误。

请参阅-https://vuejs.org/v2/guide/computed.html#Computed-Caching-vs-Methods

© www.soinside.com 2019 - 2024. All rights reserved.