itsource

Nuxt 가져오기 내에서 이름이 지정된 경우에는 Vuex 작업이 작동하지 않음

mycopycode 2022. 8. 19. 21:03
반응형

Nuxt 가져오기 내에서 이름이 지정된 경우에는 Vuex 작업이 작동하지 않음

저는 저만의 작은 프로젝트를 만들고 있습니다.nuxt fetch 메서드 내의 메인스토어(index.js)에서 스테이트에 액세스하려고 하면 모두 정상적으로 동작하지만 namesched(store/photos.js) 스토어에서 액세스하려고 하면 동작하지 않습니다.여기 제 코드가 있습니다.

store/index.js (작업)

export const state = () => ({
    fetchedData: []
})


export const mutations = {
    setData: (state, data) => {
        state.fetchedData = data;
    }
}

export const actions = {
    async get(vuexContext) {
        const requestedData = await this.$axios.get("https://jsonplaceholder.typicode.com/users");
        vuexContext.commit('setData', requestedData.data);
    },
}

내 컴포넌트:

<script>
import { mapState, mapActions } from 'vuex'
export default {
  
  async fetch({ error,store })
  {
    try {
      await store.dispatch('get');
    } catch (error) {
      console.log(error);
    }   
  },
  computed: {
    ...mapState(['fetchedData'])
  }
};
</script>

store/photos.js (동작하지 않음)

export const state = () => ({
  list: []
});

export const mutations = {
  setPhotos(state, data) {
    state.list = data;
  }
};

export const actions = {
  async getPhotos(vuexContext, context) {
    const requestedData = await this.$axios.get(
      "https://jsonplaceholder.typicode.com/photos"
    );
    vuexContext.commit("setPhotos", requestedData.data);
    
  }
};

같은 컴포넌트지만 수정됨

<script>
import { mapState, mapActions } from 'vuex'
export default {
  
  async fetch({ error,store })
  {
    try {
      await store.dispatch('photos/getPhotos');
    } catch (error) {
      console.log(error);
    }      
  },
  computed: {
    ...mapState({
      list : 'photos/list'
    })
  }
};
</script>

잘 부탁드립니다.

 namespaced: true,

이것을 index.js 파일에 추가할 수 있습니다.이게 먹히길 바라.

참조 링크:이거 드셔보세요

언급URL : https://stackoverflow.com/questions/65462552/vuex-action-not-works-with-namespaced-inside-nuxt-fetch

반응형