找不到 Vuex Store getter 函数
Vuex Store getter function not found
这是我在 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);
正在运行。
computed
属性不是 methods
,因此它们不会被调用。它的行为类似于 setters
和 getters
this.getUser
是一个计算出来的 属性,更像是一个值本身。当您将 ()
放在它之后以使其调用时,您试图将其视为 method
。这是不可能的,因为它不是 fn
,这就是出现该错误的原因。
参考这个 - https://vuejs.org/v2/guide/computed.html#Computed-Caching-vs-Methods.
Vuex getters/states
被包含在 vue instances
的 computed
属性中,因为他们的工作本身就是 return 和 value
。然而 mutations/actions
得到 include inside methods
属性,因为它的工作是 perform
一个操作示例 - 发出 async
请求并更新 state
这是我在 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);
正在运行。
computed
属性不是 methods
,因此它们不会被调用。它的行为类似于 setters
和 getters
this.getUser
是一个计算出来的 属性,更像是一个值本身。当您将 ()
放在它之后以使其调用时,您试图将其视为 method
。这是不可能的,因为它不是 fn
,这就是出现该错误的原因。
参考这个 - https://vuejs.org/v2/guide/computed.html#Computed-Caching-vs-Methods.
Vuex getters/states
被包含在 vue instances
的 computed
属性中,因为他们的工作本身就是 return 和 value
。然而 mutations/actions
得到 include inside methods
属性,因为它的工作是 perform
一个操作示例 - 发出 async
请求并更新 state