IT이야기

Vuex/Vues의 작업에서 다른 작업을 호출하는 방법

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

Vuex/Vues의 작업에서 다른 작업을 호출하는 방법

나는 호출까지 이들을 삭제하신 후 사이트의 목록을 업데이트하려고 하고 있어요.getSites()아래와 같은 방법

코드

import http from 'services/http.service';
import logging from 'services/logging.service';

const sites = http('sites', localStorage);

export default {
    getSites({dispatch}) {
        console.log('getSites')
        sites.all().then((response) => {
            dispatch('setSites', response.data.results);
        });
    },
    deleteSite({dispatch}, site) {
        return sites.delete(site).then(() => {
            this.getSites()  // <-------- doesn't works
        });
    },
};

다음과 같은 오류가 발생함

삭제 실패 ReferenceError: getSite가 정의되지 않음

질문.

새 항목 목록 가져오기를 호출하려면 어떻게 해야 하는가?또는 구성 요소 내에서 수행할 것인가?then()?

actions.js에서 호출을 제거하고 구성 요소 내에서 호출을 수행했다.

import actions from 'vuex/actions';

export default{
    // …
    methods: {
        // …
        delete_site(site){
            return this.deleteSite(site).then(response => {
                this.getSites();  // <----------- call from here
            });
        },
    vuex: {
        actions: {
            getSites: actions.getSites,
            deleteSite: actions.deleteSite,
        }
    }
}

참조URL: https://stackoverflow.com/questions/38980267/how-to-call-another-action-from-actions-in-vuex-vues

반응형