Trending October 2023 # How Refs Work In Vue.js # Suggested November 2023 # Top 15 Popular | Saigonspaclinic.com

Trending October 2023 # How Refs Work In Vue.js # Suggested November 2023 # Top 15 Popular

You are reading the article How Refs Work In Vue.js updated in October 2023 on the website Saigonspaclinic.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How Refs Work In Vue.js

Introduction to chúng tôi refs

refs in chúng tôi is defined as the referencing to make the DOM element selectable by making the key in the parent $refs and considered to be a Vue Instance property. It tells how to directly access the child functions on parent elements using this attribute and they are the most reliable attributes to reach into our templates and grab an element easily meanwhile accessing the data for that element. When a Simple DOM element is used the reference is done with that element. This Special attribute has the beauty to directly access the DOM element to fetch some data.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

The general syntax is given as:

How do refs work in Vue.js?

refs is not a standard HTML attributes and it is used only in Vue. When a ref attribute is added to an HTML element through Vue template, then we shall reference that element or in other case taking a child element in the Vue Instance. Generally, it is a read-only attribute and returns an Object. When an element uses ref attribute it automatically creates a link on itself inside this keyword. In other words, when Vue uses refs – say like if it is used for v-for loop, Vue dynamically creates an array of elements and if it is used in a component it refers the component instance. When we use refs in v-for, Vue automatically collects the DOM elements for iterations purpose and store them in an array.

Let’s see how refs work with simple code by accessing the DOM node.

Code:

export default { data: function () { return { query: “” }; }, mounted: function() { this.$refs[“surf”].focus(); } };

Above code explains that when we use ref attribute to input element which is accessed inside-mounted with this .$ ref [“surf”].

Examples of chúng tôi refs

Given below are the examples of chúng tôi refs:

Example #1

Showing simple example on HTML element with refs.

.html

{{ $refs.ab.msg }}

.js

var dem = Vue.extend({ name: 'dem', data: function() { return { msg: 'Welcome' } } }) var rrr = new Vue({ el: '#rrr' })

Explanation:

Output:

Example #2

V-ref on V-for Directive

.html

<li v-for=”(obj, index) in dataObj” :key=”`function${index}`” :ref=”`${funcRef(index)}`”

new Vue({ el: "#app", data: { dataObj: [ { even: true, item: "apple" }, { even: false, item: "Mango" }, { even: true, item: "Pomegranate" }, { even: false, item: "Guava" }, { even: true, item: "Cherry" }, { even: false, item: "Tomato" }, { even: true, item: "Avacoda" } ] }, methods: { funcRef(index) { return (index % 2 === 0 ) ? 'funcItem-mod-2' :`funcItem-${index}`; }, addClassToRef(refName, className){ } }, mounted() { this.addClassToRef('expr-1', 'odd'); this.addClassToRef('expr-2', 'even'); this.addClassToRef('funcItem-mod-2','mod-2'); } });

Explanation:

Reference Access the data Object through List-items.

Output:

Example #3

Vue refs on handling conditionals

.html

chúng tôi

import Vue from 'vue' import App from './App.vue' Vue.config.productionTip = false new Vue({ }).$mount('#app')

chúng tôi

export default { name: ‘Test’, data(){ return { } }, methods: { submit(){ console.log(this.$refs) } } } p , input, button{ font-size: 22px; } input, button{ font-size: 22px; } ul { list-style-type: none; padding: 1; } li { display: inline-block; margin: 0 11px; } a { color: blanchedalmond; }

chúng tôi

import Test from ‘./components/Test.vue’ export default { name: ‘app’, components: { Test } } #app { font-family: ‘Avenir’, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: yellowgreen; margin-top: 50px; }

Here in the above code, we use v-for directive so instead of returning objects, refs of array elements return a list of items. When we execute the above code in Visual Code, we get the output like this.

Output:

Example #4

Using input type

chúng tôi

export default { name: ‘Test’, data(){ return { counter: 1 } }, methods: { submit(){ this.counter++; console.log(this.$refs) } } } p , input, button{ font-size: 18px; } input, button{ font-size: 15px; } ul { list-style-type: none; padding: 1; } li { display: inline-block; margin: 0 11px; } a { color: blueviolet; }

Explanation:

In the above code, we have used a method submit() in which the refs are made to the input and the count value is incremented each time when we input something in the button.

Output:

Note: From all the above discussion, we conclude that ref() is used just for primitives.

Conclusion

This article shows how to reference HTML elements – DOM in Vue. All these are done by all the HTML elements properties like child node, attributes. We have also seen how to achieve this various example. Finally, this refs gets famous after the Vue instance has declared and most importantly they directly manipulate Child nodes.

Recommended Articles

This is a guide to chúng tôi refs. Here we discuss the introduction, how refs work in chúng tôi along with examples respectively. You may also have a look at the following articles to learn more –

JavaScript object.is()

JavaScript Iterate Array

JavaScript Array Contain

JavaScript onkeyup

You're reading How Refs Work In Vue.js

Update the detailed information about How Refs Work In Vue.js on the Saigonspaclinic.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!