[Vuejs]-Is it possible to manage local state within Vue component by Vuex

0👍

yes, it is most definitely possible. i’ve never used the class-based syntax, but in object based syntax, it would look something like this

<div>
  <p>{{ state1 }}<p>
  <p>{{ state2 }}</p>
  <p>{{ localState }}</p>
</div>
export default {
  computed: {
    state1() {
      return this.$store.state.state1;
    }
    state2() {
      return this.$store.state.state2;
    }
  },
  data() {
    return {
       localState: 3
    }
  },
  props: {
    componentId: {
      type: Number
  },
}

again, ive never used the class base syntax, but it might look like this pseudocode

import { Prop } from 'vue-class-component'

export default class App extends Vue {
  get state1() {
    return this.$store.state.state1;
  }
  get state2() {
    return this.$store.state.state2;
  }

  localState = 3

  @Prop()
  componentId: Number
}

Leave a comment