43

I have a template like this:

<template>
  <div>{{hello}}</div>
</template>
<script>
export default {
  data() {
    hello: "Hello World!",
    secret: "The answer is 42"
  }
}
</script>

How can I get, and manipulate the value of secret from the Chrome debugging console?

I have installed the Vue extension which allows me to look at my component and see the variables. What I would like to do is to get these variables and manipulate them.

Is this possible?

6
  • If you are running vue in debug/test mode, have you tried accessing accessing the values in the console, assigning a new value, etc Commented Aug 14, 2018 at 19:32
  • 1
    I haven't tried, I don't know how to get the values. Is this something like window.vue.mycomponent.data.foo? Commented Aug 14, 2018 at 19:33
  • Directly in the console assign a value to the vue variable or prop defined in the template Commented Aug 14, 2018 at 19:35
  • 1
    I don't understand your point. In my console I have this prompt: >. What should I write in the case I want to change the value of secret? Commented Aug 14, 2018 at 19:36
  • 1
    secret is not a global variable it belongs to a Vue component Commented Aug 14, 2018 at 19:38

6 Answers 6

40

There is a similar discussion on the Vue.js forum. Specifically, SevenLines post on Mar 25 2018 reads:

Update from 2018. If you use vue-devtools, you can access Vue from console by selecting desired component, and then accessing it through $vm variable. Check the image with example:

Example image

Sign up to request clarification or add additional context in comments.

4 Comments

Ok... the answer is then $vm0.secret then.
I don't think this answer works any longer. I get: $vm is not defined.
For me this answer still works today.
I no longer get $vm is not defined which I did a few months ago (or perhaps I was using the vite plugin "Vue devtools" instead of the browser extension, I cannot remember). Anyway it's now working, although I seem to have to access data properties via $vm.ctx.someProperty rather than just $vm.someProperty which this answer suggests is possible. Is that the same for you?
38

I use this

// if result is single element
document.getElementById('app').__vue__

// if result is multiple elements
document.getElementsByClassName('some-class')[0].__vue__

assuming your using id #app for your entry

can also do it in other elements as long as identified as Vue Component element

and get element by tag/id/class

2 Comments

I am not able to get this in production mode though... What could be the reason and what could the solution be?
With Vue3 it seems to be __vue_app__, not __vue__. document.getElementById('app').__vue_app__
5

Here's what I put in the Console, and it immediately modifies the data shown on the page:

myapp = app.__vue__.$children[0]
myapp.hello = 'Bonjour'
myapp.hello = 'Buenos dias'

Mysteriously, if you skip assigning the object to a variable, it doesn't work as intended:

app.__vue__.$children[0].hello = 'Bonjour'       // first time it works
app.__vue__.$children[0].hello = 'Buenos dias'   // second time it doesn't work

3 Comments

any idea why the autocomplete doesn;t work when not assigned to a variable. Weird!!
This appears to leverage some sort of shorthand in devtools; using app actually seems to be doing document.getElementById('app') under the hood, and returns an HTML element with an id attribute matching the typed name– in this case, the element you've rooted your Vue instance in.
This answer is out of date unfortunately. No longer works for Vue 3.
3

At least with the "vue.global.js" VueJS 3.3.11 build, you can access the Vue instance by doing:

const el = document.getElementById('#myvue');
// replace "#myvue" with the ID of the element you mounted your component using the "mount" method
const secret = el.__vue_app__._instance.proxy.secret;
// if you want to access the "secret" data

el.__vue_app__._instance.proxy is eq to el.__vue__ in VueJS 2.x

2 Comments

Well it's long-winded, but it's the only answer here that works, as of May 2024.
Based on all the competing solutions on here, it seems like the answer must change every other Tuesday or something, but this answer does work for me. If you have a global reference to app you can also do app._instance.proxy to avoid first selecting the root element of the app.
2

In 2023, Vue 3, to access:

  • data: $vm.data.yourProperty
  • computed: $vm.ctx.yourProperty
  • props: $vm.props.yourProperty

It works with vm0 too, since they are aliases for the last component click on DevTools

console

1 Comment

$vm0 and $vm are undefined for me. Can you elaborate on "they are aliases for the last component click on DevTools"?
0

in a vue3 app, the following will get you not only the component, but any variables declared in <script setup> (if your component is an SFC):

// get the component in the DOM
const domComponent = document.getElementById("my-component");
// get the vue component instance
const vueComponent = domComponent.__vnode.component;
// get the setup variables
const state = vueComponent.setupState;

1 Comment

Unfortunately when I try this on my root component .__vnode is undefined. However it does appear to sort of work on child components, but I had to use __vnode.ctx.data instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.