Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In the webpack-simple Vue.js template, how can I access the Vue app from the console?

Tags:

vue.js

vuejs2

I've created a Vue.js app using vue-cli with the webpack-simple template, and it works great. In the provided main.js, I changed the new Vue(... line to var vm = new Vue(..., so that I could access the Vue instance from the Chrome Dev Console, but the variable vm still shows as undefined.

What is the correct way for me to get a reference to the Vue object so that I can do things like manually generating events in components, or manually modifying data from the console?

like image 552
Christopher Shroba Avatar asked Jan 24 '17 15:01

Christopher Shroba


2 Answers

Try with window.vm = vm;

var vm  = new Vue({
  el: '#app',
  store,
  template: '<App/>',
  components: { App }
})

window.vm = vm;

And than just type vm in console. Your Vue object will be available now.

like image 191
t_dom93 Avatar answered Oct 08 '22 20:10

t_dom93


Another option is:

  1. Do not modify main.js / main.ts generated by vue-cli
  2. Install Vue.js devtools (available for Firefox and Chrome)
  3. On your Vue-powered website press F12 (open dev console) and go to Vue tab
  4. Every component gets assigned to $vmX variable which you can use in the console.

enter image description here

like image 20
dominik Avatar answered Oct 08 '22 20:10

dominik