it-source

vuex 저장소에서 "Uncaught TypeError: 정의되지 않은 속성 'get'을 읽을 수 없음"을 어떻게 해결할 수 있습니까?

criticalcode 2023. 6. 20. 21:41
반응형

vuex 저장소에서 "Uncaught TypeError: 정의되지 않은 속성 'get'을 읽을 수 없음"을 어떻게 해결할 수 있습니까?

내가 노력하면this .$session.get(SessionKeys.Cart)다음과 같은 내 구성요소에서:

    <template>
        ...
    </template>
    <script>
        ...
        export default {
            ...
            methods: {
                add(item) {
                    console.log(this.$session.get(SessionKeys.Cart)
                    ...
                }
            }
        }
    </script>

그건 효과가 있다.세션 카트 가져오기 성공

하지만 이렇게 myvuex 스토어에서 시도해보면,

    import { set } from 'vue'
    // initial state
    const state = {
        list: {}
    }
    // getters
    const getters = {
        list: state => state.list
    }
    // actions
    const actions = {
        addToCart ({ dispatch,commit,state },{data})
        {
            console.log(this.$session.get(SessionKeys.Cart))
            ...
        }
    }
    // mutations
    const mutations = {
        ...
    }
    export default {
        state,
        getters,
        actions,
        mutations
    }

오류가 있습니다.Uncaught TypeError: Cannot read property 'get' of undefined

이 오류를 어떻게 해결할 수 있습니까?

구성 요소를 전달할 수 있습니다.this페이로드를 사용한 디스패치라고 하는 디스패치 기능으로 이동합니다.이와 같이:

<template>
    ...
</template>
<script>
    ...
    export default {
        ...
        methods: {
            this.$store.dispatch('addToCart', { data: {}, ctx: this })

            // add(item) {
            //    console.log(this.$session.get(SessionKeys.Cart)
            //    ...
            //}
        }
    }
</script>

import { set } from 'vue'

// initial state
const state = {
    list: {}
}

// getters
const getters = {
    list: state => state.list
}

// actions
const actions = {
    addToCart ({ dispatch, commit, state }, { data, ctx })
    {
        console.log(ctx.$session.get(SessionKeys.Cart))
        ...
    }
}

// mutations
const mutations = {
    ...
}

export default {
    state,
    getters,
    actions,
    mutations
}

당신은 이걸 할 수 있다.

import Vue from 'vue’

const session = Vue.prototype.$session;

당신의 가게의 맨 위에 그리고 당신의 행동 방식에.

console.log(session.get(SessionKeys.Cart))

이것은 효과가 있을 것입니다.

언급URL : https://stackoverflow.com/questions/52529345/how-can-i-solve-uncaught-typeerror-cannot-read-property-get-of-undefined-in

반응형