vuejs2: how can i destroy a watcher?
Asked Answered
S

3

12

How can i destroy this watcher? I need it only one time in my child component, when my async data has loaded from the parent component.

export default {
    ...
    watch: {
        data: function(){
            this.sortBy();
        },
    },
    ...
}

gregor ;)

Stagg answered 28/10, 2017 at 8:47 Comment(2)
How about an if/else inside the watcher, which is accessing a data attribute?Scale
yeah at the moment i was doing it with a second property "loaded", which i turned true when the ajax call was done // child watch: { loaded: function(){ this.sortBy(); }, }, //parent <thumbnailList :loaded=loaded :data=data :href=section></thumbnailList> photosApi.getGalleries(this.id).then((response) => { this.loaded = true; this.data = response.data; }).catch((error) => { because the data object gets later modified and then it shouldn't invoke the "sortBy" functionStagg
C
32

If you construct a watcher dynamically by calling vm.$watch function, it returns a function that may be called at a later point in time to disable (remove) that particular watcher.

Don't put the watcher statically in the component, as in your code, but do something like:

created() {
   var unwatch = this.$watch(....)
   // now the watcher is watching and you can disable it
   // by calling unwatch() somewhere else; 
   // you can store the unwatch function to a variable in the data
   // or whatever suits you best 
} 

More thorough explanation may be found from here: https://codingexplained.com/coding/front-end/vue-js/adding-removing-watchers-dynamically

Cubeb answered 28/10, 2017 at 9:19 Comment(1)
If anyone wants the Vue 3 docs for this vuejs.org/guide/essentials/watchers.html#stopping-a-watcherFormula
B
5

Here is an example:

<script>
export default {
  data() {
    return {
      employee: {
        teams: []
      },
      employeeTeamsWatcher: null,
    };
  },

  created() {
    this.employeeTeamsWatcher = this.$watch('employee.teams', (newVal, oldVal) => {
      this.setActiveTeamTabName();
    });
  },

  methods: {
    setActiveTeamTabName() {
      if (this.employee.teams.length) {
        // once you got your desired condition satisfied then unwatch by calling:
        this.employeeTeamsWatcher();
      }
    },
  },
};
</script>
Badger answered 15/7, 2019 at 23:50 Comment(0)
D
4

If you are using vue2 using the composition-api plugin or vue3, you can use WatchStopHandle which is returned by watch e.g.:

    const x = ref(0);

    setInterval(() => {
      x.value++;
    }, 1000);

    const unwatch = watch(
      () => x.value,
      () => {
        console.log(x.value);
        x.value++;

        // stop watch:
        if (x.value > 3) unwatch();
      }
    );

For this kind of stuff, you can investigate the type declaration of the API, which is very helpful, just hover the mouse on it, and it will show you a hint about what you can do: enter image description here

Deciare answered 28/1, 2021 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.