Vue.js computed property not updating

前端 未结 7 725
猫巷女王i
猫巷女王i 2020-12-29 22:36

I\'m using a Vue.js computed property but am running into an issue: The computed method IS being called at the correct times, but the value returned by the computed

相关标签:
7条回答
  • 2020-12-29 23:08

    I have a workaround for this kind of situations I don't know if you like it. I place an integer property under data() (let's call it trigger) and every time the object that I used in computed property changes, it gets incremented by 1. So, this way, computed property updates every time the object changes.

    Example:

    export default {
    data() {
      return {
        trigger: 0, // this will increment by 1 every time obj changes
        obj: { x: 1 }, // the object used in computed property
      };
    },
    computed: {
      objComputed() {
        // do anything with this.trigger. I'll log it to the console, just to be using it
        console.log(this.trigger);
    
        // thanks to this.trigger being used above, this line will work
        return this.obj.y;
      },
    },
    methods: {
      updateObj() {
        this.trigger += 1;
        this.obj.y = true;
      },
    },
    };
    

    here's working a link

    0 讨论(0)
提交回复
热议问题