Vuex 将文件 getters.js 和 getter 与参数分开
Vuex separate file getters.js and getter with argument
我有单独的文件getters.js:
export const getDate = (state, format) => {
const getDateFormat = (state, format) => {
return moment(state.date).format(format)
}
return getDateFormat(state, format)
}
我在组件内部使用此方法getDate
:
computed:{
...mapGetters({
getDate: 'getDate',
}),
getDateFormat() {
return this.getDate('dd-mm-yyyy')
},
},
但是 Vue return 我的错误:
this.getDate is not a function
您不能将自定义参数传递给 getter。 getter 的参数:
state, getters, rootState
getItem(state, getters, rootState) {
}
好的,我解决了,我将我的方法更改为箭头函数:
export const getDate = state => format => {
//...
}
我有单独的文件getters.js:
export const getDate = (state, format) => {
const getDateFormat = (state, format) => {
return moment(state.date).format(format)
}
return getDateFormat(state, format)
}
我在组件内部使用此方法getDate
:
computed:{
...mapGetters({
getDate: 'getDate',
}),
getDateFormat() {
return this.getDate('dd-mm-yyyy')
},
},
但是 Vue return 我的错误:
this.getDate is not a function
您不能将自定义参数传递给 getter。 getter 的参数:
state, getters, rootState
getItem(state, getters, rootState) {
}
好的,我解决了,我将我的方法更改为箭头函数:
export const getDate = state => format => {
//...
}