admin管理员组

文章数量:1355670

I would like to disable watcher, so it doesn't trigger certain watcher when created or mounted finish and enable it after these done. What is the proper way to achieve this?

I would like to disable watcher, so it doesn't trigger certain watcher when created or mounted finish and enable it after these done. What is the proper way to achieve this?

Share Improve this question asked Dec 31, 2017 at 11:34 tomJOtomJO 3713 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Without seeing what you are doing, it's quite difficult to know exactly what you are trying to achieve. Generally, you should not be handling watchers yourself, so it's worth taking a look at your code to see if there is a better solution. With that said, it is possible to apply a watcher at runtime, you can do it inside the mounted hook, after you have initialised everything else:

new Vue({
  el: '#app',
  created() {
    // this won't fire the watcher as it hasn't been applied yet
    this.foo = 'foo'
  },
  mounted() {
    // Apply watcher after all other initialization is done
    this.applyFooWatcher()
  },
  methods: {
    applyFooWatcher() {
      this.$watch('foo', function(newVal, oldVal) {
        console.log('Foo changed from ' + oldVal + ' to ' + newVal);
      });
    }
  },
  data: {
    foo: ''
  }
})

Here's the JSFiddle: https://jsfiddle/fo0vmxf6/

本文标签: