0

I'm making a captcha component using vue. I try to fetch the captcha when the component created. When I do this in a async manner, the page will not be reactive, although the state has already been updated. But I when do it in sync manner, everything is fine. So, I'm wondering why async manner won't work?

This works

<template>
  <section>
    <div class="captcha-container">
      <div v-if="captcha" v-html="captcha.data"></div>
    </div>
  </section>
</template>

<script>
import { mapState, mapActions } from "Vuex";

export default {
  created: function() {
    this.$store.commit('setCaptcha', {id: 'xx', data:'Hi'});
  },
  computed: {
    ...mapState(["captcha"])
  },
};
</script>

This does not work

<template>
  <section>
    <div class="captcha-container">
      <div v-if="captcha" v-html="captcha.data"></div>
    </div>
  </section>
</template>

<script>
import { mapState, mapActions } from "Vuex";

export default {
  created: function() {
    setTimeout(() => {
      this.$store.commit('setCaptcha', {id: 'xx', data:'Hi'});
    }, 1000);
  },
  computed: {
    ...mapState(["captcha"])
  },
};
</script>
IceFox
  • 1
  • 1
  • `v-html` does one-way binding that is why it's value doesn't change even with state update.You may need to use `v-model` for two-way data binding. Checkout `https://stackoverflow.com/questions/44376484/how-to-do-databind-two-way-in-v-html` – Isaac Obella Jan 24 '19 at 09:53
  • Actually, One way binding is my intension. The problem is: state change should be reflected on page, but did not for case 1. – IceFox Jan 24 '19 at 10:33
  • I have tested your code in codepen and it seems to work `https://codepen.io/wizlif/pen/KJpjeP` – Isaac Obella Jan 24 '19 at 11:35
  • @IsaacObella Yes, I've tried your codepen, it works fine. Then I rechecked my code, found that I declare `Vue.use(Vuex)` twice. I refactored my code, removed one of the use statement. It works now. Thanks for your help. – IceFox Jan 25 '19 at 03:50
  • Welcome @IceFox – Isaac Obella Jan 25 '19 at 05:42

0 Answers0