Background
Consider the following, very basic, plugin:
import SomeObject from "./SomeObject";
/**
* Some Cool Plugin...
*/
export default {
install(Vue){
Vue.prototype.$someObject = Vue.observable(SomeObject);
}
}
The purpose of the above is to register an object to be reactive throughout my application.
The Problem & Question
Vue.prototype.$someObject contains certain properties that need to be watched, on a global level, not a component level.
Is there a way to attach a watcher to the Vue instance within the install method of a Vue plugin?
Please note, I am not looking for the following answer, I am already aware this can be done but it defeats the point of separating the code into a plugin. A global mixin would also not be sufficient...
watch: {
'$someObject.foo': {
handler(value) {
console.log(value);
}
}
}
What I have Tried
Based on the documentation outlined here, I assumed I could do something along the lines of the following:
export default {
install(Vue){
Vue.prototype.$someObject = Vue.observable(SomeObject);
// I have tried all of the following:
Vue.watch(Vue.prototype.$someObject.foo, value => { console.log(value) });
Vue.$watch(Vue.prototype.$someObject.foo, value => { console.log(value) });
Vue.prototype.watch(Vue.prototype.$someObject.foo, value => { console.log(value) });
Vue.prototype.$watch(Vue.prototype.$someObject.foo, value => { console.log(value) });
Vue.watch(Vue.$someObject.foo, value => { console.log(value) });
Vue.$watch(Vue.$someObject.foo, value => { console.log(value) });
Vue.prototype.watch(Vue.$someObject.foo, value => { console.log(value) });
Vue.prototype.$watch(Vue.$someObject.foo, value => { console.log(value) });
}
}