Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
watching a vuex state inside nested modules and login its value in a component whenever it changes

I know a similar question has been asked here but mine is a little bit different, the state I want to watch is inside some nested modules. on the link above they suggested I can watch the state like this:

watch: {
  '$store.state.drawer': function() {
    console.log(this.$store.state.drawer)
  }
}

But my state is in nested modules as I mentioned, something like store/moduleOne/moduleTwo/moduleThree They suggested to do this:

$store.state.moduleOne.moduleTwo.moduleThree.something

But I tried it and many more variations and it didn't work! I asked on the original post and no answers!

So my question is how can I watch a state, let's call it price and let's say it's in store/moduleOne/moduleTwo/moduleThree and then log its value in my component whenever it changes using (using map functionalities is no problem and is preferred)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Assuming nested modules declared in Vuex 4 like this:

import { createStore } from 'vuex'

export default createStore({
  modules: {
    moduleOne: {
      namespaced: true,
      modules: {
        moduleTwo: {
          namespaced: true,
          modules: {
            moduleThree: {
              namespaced: true,
              state: () => ({
                price: 0,
              }),
              mutations: {
                SET_PRICE(state, price) {
                  state.price = price
                },
              },
            },
          },
        },
      },
    },
  },
})

...the watcher on the price state in moduleThree would be:

export default {
  watch: {
    '$store.state.moduleOne.moduleTwo.moduleThree.price'(price) {
      console.log({ price })
    }
  }
}

demo

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda