I am currently reading out a language setting from the dom and setting it as a global
Vue.js variable like this:
JavaScript
x
3
1
const language = document.querySelector('html').getAttribute('lang');
2
Vue.prototype.$language = language;
3
It works fine but I have also seen this as an example from a Vue.js event bus:
JavaScript
1
8
1
Object.defineProperties(Vue.prototype, {
2
$bus: {
3
get() {
4
return EventBus;
5
}
6
}
7
});
8
What is the real difference between these two and is there possible a more preferred way of doing this?
Advertisement
Answer
Object.defineProperties is can be used when you need to set property descriptors such as getters , setters ,read only etc .
But in your case using Vue.prototype.$language = language; will be the more cleaner approach.
If you are looking for Vue preferred way, this is the guide on adding instance properties to Vue instance.