6

I am using Vue 2.0 and I have an error

(Uncaught ReferenceError: vm is not defined)

when i use vm.$data or any other in console to check the properties. I have latest version of vue.js 2.2.4.

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
user3423920
  • 189
  • 1
  • 4
  • 13

5 Answers5

15

As mentioned by @Fiete the vm variable wont be available by default.

You need to define vm globally to access it from the console.

Something like this should work

var vm = new Vue({
  el: '#app',
  data: {
    a: 1
  },
  router,
  template: '<App/>',
  components: {
    App
  }
});

global.vm = vm; //Define your app variable globally

Now you can use vm.$data.PROPERTY or vm.PROPERTY.

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
  • 1
    It's because of webpack packing of js files :) here is the more detailed answer: https://stackoverflow.com/a/40416826/2842750 – Pavel Biryukov Jul 15 '18 at 04:56
2

The vm variable does not exists by default. And vuejs does not set that variable. If you want to debug your vuejs application I recommend the vuejs Chrome extension.

By opening the extension on your page you can select the component you want to debug. After that you can access the selected component by using the $vm0 variable.

Fiete
  • 1,332
  • 11
  • 12
0
let app = new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')


global.vm = app;

in anywhere,

globalThis.vm.$subscribeTo...
serkan
  • 6,885
  • 4
  • 41
  • 49
0

vm looks like referring to the Vue instance, then just use this.$set instead of vm.$set. (Worked on a project generated with @vue/cli)

Mathias Osterhagen
  • 402
  • 1
  • 3
  • 19
0

It's really old, but for me the issue was that $vm would become unavailable when I was not focused on the tab / had different tab open. I just had to re-focus on the tab for which the devtools was open and $vm would appear again.

If just re-focusing won't do the trick, sometimes it helps to roam around Vue devtools Components tab until you see $vm written along the component names.

enter image description here

DemiA
  • 333
  • 1
  • 11