programing

Vue에서 Vue를 사용하여 mapState를 사용하여 데이터를 가져올 수 없음

sourcetip 2022. 8. 8. 23:04
반응형

Vue에서 Vue를 사용하여 mapState를 사용하여 데이터를 가져올 수 없음

mapState와 좀 헷갈리네요...이것 좀 봐

index.vue...

    computed: {

        //this doesn't work
        ...mapState(['user']), 

        // this works 
        user () { 
            return this.$store.state.data.user
        }
    }

모듈/data.module

...

   const state = {
        msg: 'Tom'
    }
...

아마...mapState(['user'])돌아온다user () { return this.$store.state.user}없이.data이 스레드에서는 LinusBorg와 같은 오브젝트가 설명합니다.https://forum.vuejs.org/t/dont-understand-how-to-use-mapstate-from-the-docs/14454/9

자세한 코드는 이쪽에서 확인하실 수 있습니다.https://codesandbox.io/s/n5z02km81l

스토어는 모듈로 나누어져 있기 때문에 오브젝트 구문을 사용하여mapState서브모듈에서 스테이트에 액세스하려면:

...mapState({
  msg: state => state.data.msg
})

https://codesandbox.io/s/nn0zo023mm

아니면, 그 다음에data스토어 이름 지정, 즉 추가namespaced: true,로.data.js모듈, 다음으로 액세스합니다.

...mapState('data', {
  msg: state => state.msg
})

https://codesandbox.io/s/olp064qm55

잘 안 돼요 왜냐하면user의 특성입니다.data

당신은 당신의 스토어와 우리에 getter를 생성해야 합니다.mapGetters컴포넌트에 추가해 주세요.

https://vuex.vuejs.org/guide/getters.html


export default store = new Vuex.Store({
  state: { ... }
  getters: {
    user => (state) => state.data.user
  }
}

요소

import { mapGetters } from 'vuex'

export default {

  computed: {
    ...mapGetters(['user'])
  }
}

언급URL : https://stackoverflow.com/questions/54791157/i-cant-any-get-data-using-mapstate-in-vue-using-vuex

반응형