D
D
danilr2020-02-24 10:13:53
Vue.js
danilr, 2020-02-24 10:13:53

How to subscribe to a state change in Vuex in a component?

There is a property in state Vuex, when changing which I need to execute a method in the component, how to do it correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima Pautov, 2020-02-24
@danilr

<template>
  <div>
    {{ myProp }}
  </div>
</template>

import { mapState } from 'vuex'

export default {
  computed: {
    ...mapState([
      'myProp',
    ]),
  },
  watch: {
    myProp () {
      this.myMethod()
    },
  },
  methods: {
    myMethod () {
      console.log('Hello, world!')
    },
  },
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question