반응형
vuex 작업에 dayJS를 사용할 수 있지만 Vuex 저장소의 상태를 초기화할 수 없는 이유는 무엇입니까?
NuxtJs를 사용하여 초기 월을 현재 월로 설정하려고 합니다.dayjs
모듈.
사용할 수 있는 이유this.$dayjs
행동을 취하지만 상태는 아닐까요?글로벌하게 접근할 수 있어야 하지 않을까요?
그리고 이번 달은 어떻게 초기화하면 좋을까요?
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({ }) {
},
}
이 간단한 예에서는,undefined
에러입니다.this
인 것 같다undefined
.
그state
Nuxt 인스턴스는 아직 정의되지 않았습니다.그리고.this
"작업" (즉, Nuxt 인스턴스의 경우)bindOngepland
호출되었을 때 컨텍스트가 바인딩된 일반 함수이기 때문에 동작입니다.
회피책은 컴포넌트가 상태를 초기화하는 액션을 호출하는 것입니다.유니버설 모드(또는 )에서는 스토어가 자동으로 호출되어 상태를 초기화하는 액션을 제공할 수 있습니다.
// store/index.js
export const actions = {
nuxtServerInit({ commit }) {
commit('SET_MONTH', this.$dayjs().startOf('month'))
}
}
export const mutations = {
SET_MONTH(state, value) {
state.month = value
}
}
SPA 모드(ssr: false
), 액션을 명시적으로 디스패치해야 합니다.
// store/index.js
export const actions = {
init({ commit }) {
commit('SET_MONTH', this.$dayjs().startOf('month'))
}
}
export const mutations = {
SET_MONTH(state, value) {
state.month = value
}
}
// MyComponent.vue
export default {
mounted() {
this.$store.dispatch('init')
}
}
언급URL : https://stackoverflow.com/questions/65244446/why-can-i-use-dayjs-in-vuex-actions-but-not-to-initialize-states-in-a-vuex-store
반응형
'itsource' 카테고리의 다른 글
MariaDB(mysql-config-editor에 상당)의 어디에 패스워드/로그인 패스를 저장합니까? (0) | 2022.09.11 |
---|---|
문자열이 지정된 문자열로 시작되는지 확인하려면 어떻게 해야 합니다. (0) | 2022.09.11 |
Python에서 예외 값을 가져오는 중 (0) | 2022.09.11 |
JavaScript에서 "0"은 false와 같지만, "if"에 의해 테스트되면 그 자체로 false가 아닌 이유는 무엇입니까? (0) | 2022.09.11 |
인덱스 서명 매개 변수 형식은 유니언 형식일 수 없습니다.대신 매핑된 개체 유형을 사용하는 것이 좋습니다. (0) | 2022.09.11 |