Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue.js 'document.getElementById' shorthand

Does vue.js have a shorthand for document.getElementById('#id') like JQuery's $('#id')?

If so, where is the reference for this in the docs so I can locate other information?

like image 828
Mike Thrussell Avatar asked May 01 '16 18:05

Mike Thrussell


People also ask

Can we use document getElementById in VUE JS?

To use document. getElementById in Vue. js, we can assign a ref to the element we want to get. And then we can use this.

Why We Use $V in VUE JS?

$v is an object that calls vuelidate (at the time of writing the comment, supported in version Vue. js 2.0), which is intended to check every input, which is made in a non-html form.

What is the shorthand for V bind in Vue?

In the shorthand, everything before the argument (i.e. v-bind: ) is condensed into a single character, : . Here the argument is the event name to listen to: click . v-on has a corresponding shorthand, namely the @ character.

How do I use $refs in VUE JS?

Vue can store references to DOM elements in a property called $ref . The first thing we have to do, is add a ref attribute to the element we want to reference in our Javascript. The value of the ref attribute will be the name of it within the $ref property.


1 Answers

Theres no shorthand way in vue 2.

Jeff's method seems already deprecated in vue 2.

Heres another way u can achieve your goal.

 var app = new Vue({      el:'#app',      methods: {              showMyDiv() {             console.log(this.$refs.myDiv);          }      }           });
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>  <div id='app'>     <div id="myDiv" ref="myDiv"></div>     <button v-on:click="showMyDiv" >Show My Div</button>  </div>
like image 61
胡亚雄 Avatar answered Sep 22 '22 01:09

胡亚雄