Disable watch in Vue 2 when mutating data
This is a mixin for Vue 2
export default {
methods: {
$withoutWatchers(cb) {
const watcher = {
cb: this._watcher.cb,
sync: this._watcher.sync,
};
this._watcher = Object.assign(this._watcher, { cb: () => null, sync: true });
cb();
this._watcher = Object.assign(this._watcher, watcher);
},
},
};
You can then use the method by passing in the mutation
methods: {
sample () {
this.$withoutWatchers(() => {
// mutation goes here
// ex: this.foo = "bar";
})
}
},