IT이야기

NuxtJS 저장 상태 변수가 정의되지 않은 상태로 계속 반환됨

cyworld 2022. 5. 18. 21:59
반응형

NuxtJS 저장 상태 변수가 정의되지 않은 상태로 계속 반환됨

나는 NuxtJs 매장에 문제가 있다. 항상 정의되지 않은 채로 반품한다. 나는 비슷한 질문을 많이 찾았지만 아무도 문제를 해결하지 못했다.여기 내 암호야:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state(){ 
        return{
            count: 500
        }
    },
    getters: {
        getCount: (state) => {
            state.count
        }
    },
})

그리고 나서 나는 이렇게 하도록 노력했다.

this.$store.getters.getCount

이것 또한 시도했다.

computed: {
        ...mapGetters(['getCount'])
},
created(){
        console.log("count: "+this['getCount'])
}

오류는 다음과 같다.

Nuxt는 당신을 위해 자동으로 상점을 설정하므로 당신은 당신의 상점을 인스턴스화할 필요가 없다.

설정 코드 제거:

// REMOVE ALL THIS:
//
// import Vue from 'vue'
// import Vuex from 'vuex'
//
// Vue.use(Vuex)
//
// const store = new Vuex.Store({/*...*/})

그리고 창조하다store/index.js다음 내용 포함:

export const state = () => ({
  count: 500,
})

export const getters = {
  getCount: state => state.count,
}

이제 구성 요소의 스토어에 액세스할 수 있음:

export default {
  mounted() {
    console.log('count', this.$store.getters.getCount)
  }
}

데모를 하다

참조URL: https://stackoverflow.com/questions/68147509/nuxtjs-store-state-variables-keeps-returning-undefined

반응형