A
A
Andrew2020-03-06 23:42:59
Vue.js
Andrew, 2020-03-06 23:42:59

Is it possible to ignore the watch firing during component initialization without introducing additional variables?

Hey!

data() {
        return {
            some_value: null
        }
    },
    watch: {
        some_value(value) {
            // code
        }
    },
    mounted() {
        getAsyncData() 
    },
    methods: {
        async getAsyncData() {
            this.some_value = await api.getSomeValue()
        }
    }


The standard vue component, when initialized, the watcher will work as expected - the variable has changed from null to the received value. Is there a way to make it not fire on initialization? If the solution is only with the help of a separate variable (if (notTheFirstTime) { // code }), then I would be glad if someone suggested how these variables are called in such cases.

It is necessary to ignore the first call because after changing the monitored value, the data save method is called via debounce. Making a pull request and then a second update request is extremely unwise.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-03-06
@AndrewRusinas

Start monitoring after receiving data:

async created() {
  await this.getAsyncData();
  this.$watch('some_value', value => {
    ...
  });
},

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question