【发布时间】:2021-03-22 11:04:42
【问题描述】:
我正在尝试使用 NuxtJS dayjs 模块将初始月份设置为当前月份。
为什么我可以在操作中使用this.$dayjs,但不能在状态中使用?
它不应该是全球可访问的吗?
我怎样才能在 state 中初始化当前月份?
export const state = () => ({
month: this.$dayjs().startOf('month'), //THIS LINE DOESNT WORK
})
export const mutations = { }
export const actions = {
bindOngepland: firestoreAction(function ({ bindFirestoreRef, rootState }) {
const month = this.$dayjs().startOf('month') // THIS LINE DOES WORK
const nextMonth = state.month.add(1, 'month')
}),
setNextMonth({ }) {
},
}
在这个简化的示例中,第 2 行出现 undefined 错误。this 似乎是 undefined。
【问题讨论】: