search

Home  >  Q&A  >  body text

javascript - How to elegantly obtain the value of state in vuex in vue component

This is how I wrote it, it feels a bit strange

The official API says this, so how do I assign the value in state to a in data

伊谢尔伦伊谢尔伦2788 days ago957

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-07-05 10:53:26

    Remove a in data() and directly change it to calculated attribute

    computed: {
        a() {
            return this.$store.state.a;
        }
    }

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-07-05 10:53:26

    If your a exists in vuex, as in the document, use the calculated attribute this.getA directly without declaring a in data

    To modify the value of a, please use mutations to submit to vuex

    reply
    0
  • Cancelreply