source

Vuex가 개별 돌연변이에 가입하다

factcode 2022. 8. 18. 23:26
반응형

Vuex가 개별 돌연변이에 가입하다

개인 돌연변이에 가입할 수 있나요?

대신:

this.$store.subscribe((mutation, state) => {
   if(mutation === 'someMutation'){
       doSomething()
   }
})

다음과 같은 것을 원합니다.

this.$store.subscribe('someMutation', state => {
    doSomething()
})

Vue 시제품 어딘가에 있는 방법을 포장해 보는 것은 어떻습니까?

즉, 다음과 같은 것이 아니라

this.$store.subscribe((mutation, state) => {
   if(mutation === 'someMutation'){
       doSomething()
   }
})

다음과 같은 것이 있습니다.

Vue.prototype.subscribeMutation = function(someMutation, someFunction) {
       this.$store.subscribe((mutation, state) => {
       if(mutation === someMutation){
           someFunction(state)
       }
    })
}

코드를 테스트해 본 적은 없지만 작업 결과는 쉽게 얻을 수 있을 것입니다.

vuex 문서에서:

핸들러는 모든 변환 후에 호출되며 변환 기술자 및 변환 후 상태를 인수로 받습니다.

모든 돌연변이에 반응하기 때문에 자신의 조건만으로 실행할 수 있습니다.

다음과 같은 방법을 사용해 보십시오.

this.$store.subscribe(mutation => {
                if (mutation.type === 'setData') {
//  do something here
                }
            });

언급URL : https://stackoverflow.com/questions/45845789/vuex-subscribe-to-individual-mutation

반응형