Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access variables in a Vue component from the JS Console?

Tags:

vue.js

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?

like image 584
nowox Avatar asked Aug 14 '18 19:08

nowox


People also ask

How do I use Vue components in JavaScript?

To create a component, following is the syntax. Vue. component('nameofthecomponent',{ // options}); Once a component is created, the name of the component becomes the custom element and the same can be used in the Vue instance element created, i.e. inside the div with ids component_test and component_test1.

How do you use variables in Vue?

so you can get global variable value and use global variable in vue js. if you define global variable then you can easily access global variable in vuejs app. We will define global variables using vue js mixin. using mixin we will declare all variables in data method.


2 Answers

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

like image 142
Xixis Avatar answered Sep 23 '22 11:09

Xixis


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

like image 20
aNulliHate Avatar answered Sep 25 '22 11:09

aNulliHate