0

There are a lot of data and computed params and methods in a Vue component sometimes, all of them are interconnected. And when I change one parameter, then a few more parameters update with it (data, computed params, especcialy when some parama are changing in the Vuex actions or mutations and it difficult to track them all). And I can't get which ones exactly...

Is it possible to get all parameters which were updated in the updated()?

Please read comments in the simple example below:

<script>
export default {
    /**
     * This is Basket.vue component
     * The basket has some fruits.
     */
    data () {
        return {
            apples: 5,
            oranges: 5,
            bananas: 5
        }
    },
    /**
     * Someone randomly taking one fruit when will see the basket
     */
    mounted () {
        let randomFruit = Object.keys(this.$data).sort(() => Math.random() - 0.5); // ["oranges", "bananas", "apple"]
        this.takeFruit(randomFruit[0]);
    },
    methods : {
        takeFruit(fruit) {
            this[fruit]--;
        }
    },
    computed : {
        any_apples () {
            return this.apples > 0
        },
        any_oranges () {
            return this.oranges > 0
        },
        any_bananas () {
            return this.bananas > 0
        },
        total () {
            return this.apples + this.oranges + this.bananas
        }
    },
    /**
     * And there is a question here about updated Vue lifecycle.
     * How detect which data of the component exactly has been updated?
     * For example, we took one apple, updated params are "apples", "any_apples", "total", how can we get it here?
     */
    updated () {
        let updated_params = ... ; // how get ["apples", "any_apples", "total"]?
        console.log("What exactly updated?", updated_params);
    },
}
</script>
eleave
  • 107
  • 2
  • 9
  • Can you also add your overall goal - why do you need this information? Maybe there is a better way to achieve it - typically I'd say use a watcher where you have both values. But as said it depends on your overall use case – Frnak May 14 '20 at 12:09
  • @FrankProvost , just theory and convenience. Agree that for debug, it is very useful to know why the component is being updated and what specific data has changed on updated() lifecycle. I want to see the listing of properties due to which the component was updated. After all, the update was caused by something, what is the reason?... I'd like to know! :) – eleave May 14 '20 at 12:51
  • We can add Watchers. But to add for each property a separate watcher function is a bad idea, isn't it? – eleave May 14 '20 at 12:52
  • 1
    based on this question https://stackoverflow.com/questions/41626565/how-do-i-watch-all-keys-in-a-data-object-in-vue-2 you cann add a watcher to your data - so it basically watches everything :) – Frnak May 14 '20 at 13:21
  • @FrankProvost Watch all the $data deeply? Is it legally? :D Interesting solution. And what is the benefits? Diff **newValue** and **oldValue** objects in the loop to get the difference for each property? – eleave May 14 '20 at 13:42
  • legally ? yes ! , smart ? not sure ! performant ? probably not if you have a lot of data ! hotel ? trivago – Frnak May 14 '20 at 14:08
  • Is your goal for debug purpose or actual code? – Cristiano Soleti May 14 '20 at 15:15
  • @FrankProvost, haha, thanks, I will take a look at the Watchers! – eleave May 14 '20 at 16:50
  • @CristianoSoleti, interested in both! **For debug:** Just wondered why there is no property in **updated()** lifecycle which could show me every time in **console.log()** "your component has been updated because of this: ..., ..., ...". **For actual code:** to know did the update happen due to computed properties changes or only due vm.$data – eleave May 14 '20 at 16:50
  • 1
    Well, asking what you requested I believe is quite expensive. Definitely watchers and computed property will fire telling you what has updated, but for testing purposes I would suggest using vue devtools. https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd?hl=en – Cristiano Soleti May 14 '20 at 17:00

0 Answers0