N
N
nastya_zholudeva2018-02-14 12:22:17
Vue.js
nastya_zholudeva, 2018-02-14 12:22:17

How to make computed fire after beforeCreate?

How to make computed fire after beforeCreate. Now on the console in the browser, I see that computed is called, then beforeCreate, then computed again. Therefore, I made a crutch to check the availability of data in product. Is it possible to do something smarter?

data() {
            return {
                product: {}
            }
        },
        computed: {
            mainPhoto: function() {
                console.log('22222');
                if (Object.keys(this.product).length > 0) {
                    function compare(a, b) {
                    if (a.sort < b.sort)
                        return -1;
                    if (a.sort > b.sort)
                        return 1;
                    return 0;
                }
                this.product.photos.sort(compare);
                   return this.product.photos[0].image
                } else {
                    return
                }

            }
        },
        beforeCreate() {
            this.$nextTick(() => {
                axios.get('http://api.selfie-store.ru/products/' + this.$route.params.id).then((data) => {
                   console.log('1111');
                    this.product = data.data;
                })
            })

        },

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ramil, 2018-02-14
@rshaibakov

In fact, according to the logic of the life cycle of Vue components, everything is correct.
The documentation says beforeCreate is called synchronously right after the instance is initialized, before setting up data monitoring, tracking mechanisms, and events. That is, at this moment, computed will be executed for the first time, but there will be no tracking of data inside it yet.
I also always check in computed and it seems to me that this is normal.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question