Hello I am working with Vuejs. I have defined a global variable using the following code in main.js
Vue.prototype.$state = Vue.observable({ val: 1 });
In another file I want to access this variable inside script so I am using
export default {
methods: {
showup() {
if ($state.val === 2) $state.val = 1; else $state.val = 2;
},
},
But I am getting the error as
54:11 error '$state' is not defined no-undef
Can someone help me with this. Thanks a lot .
CodePudding user response:
You missed to add this
which refers to the Vue instance :
export default {
methods: {
showup() {
if (this.$state.val === 2) this.$state.val = 1; else this.$state.val = 2;
},
},