Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting error $state not defined in vuejs

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

54:11  error  '$state' is not defined  no-undef

Can someone help me with this. Thanks a lot .

>Solution :

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;
    },
  },
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading