Attribute Bindings in VueJS

Learn about the attribute bindings in VueJS.
Submitted by Venkatesan C, on March 09, 2022

Binding to attributes of an HTML element can be done by the v-bind directive in VueJS. The mustache syntax cannot be used inside an Html element.

Let's look at an example by creating a Vue project created by Vite. Let's name it second-app (in fact this is the same project that's been created in the VueJS installation tutorial).

Here's the project structure:

Attribute Bindings | Output (1)

Now delete everything inside the App.vue file and paste the below code. The below is simpler to understand the text interpolation.

App.vue

<template>
    <h1 v-bind:id="headingId">Hello this is just a heading</h1>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      headingId: "heading"
    };
  },
};
</script>

From the above code, you can see that we have binded headingId property to the <h1>'s id property.

Attribute Bindings | Output (2)

Look at the inspect elements tab as well. You can see that the heading tag has an id attribute with value heading as intended.

Attribute Bindings | Output (3)

Binding Boolean Attribute

Let's say you want to bind a boolean attribute to an element. Look at the following code.

App.vue

<template>
    <h1 v-bind:id="headingId">Hello this is just a heading</h1>
    <button v-bind:disabled="isDisabled">Button</button>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      headingId: "heading",
      isDisabled: true
    };
  },
};
</script>

Above we have added a button with disabled attribute bind to a property called isDisabled.

Note: With boolean attributes, if the property resulted in a true value, the attribute will be bound inside the element, else it will be removed as if it's not there.

isDisabled: true

The button is disabled in the output:

Attribute Bindings | Output (4)

Now inspecting the element will show that the attribute disabled is bound to the button as intended. You view this by the following shortcut Ctrl + Shift + I.

Attribute Bindings | Output (5)

You can see <button disabled>, because the isDisabled property is true.

Let's look at the case,

isDisabled: false

Now the button is not disabled,

Attribute Bindings | Output (6)

Inspecting the element would reveal that there is no disabled attribute in the button element as stated above.

Attribute Bindings | Output (7)

Binding Multiple Attributes

It is common to bind multiple properties to a template element. In VueJS, it is possible to bind multiple attributes to a template element by declaring it as an object and assigning it to the v-bind directive as,

v-bind="objectAttr"

Let's replace everything in the App.vue as follows,

App.vue

<template>
  <h1 v-bind:id="headingId">Hello this is just a heading</h1>
  <button v-bind:disabled="isDisabled">Button</button>
  <p v-bind="objectAttr">
    Lorem ipsum dolor sit amet consectetur adipisicing elit.
  </p>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      headingId: "heading",
      isDisabled: false,
      objectAttr: {
        id: "hero-section",
        class: "section-green",
      },
    };
  },
};
</script>

In the above code, we have included a paragraph tag and bound an object of attributes to it with the help of the v-bind directive. Vue automatically extracts the keys and values of this object and assigns these as attributes to the respective element.

Output:

Attribute Bindings | Output (8)

Inspecting the element will show us that id and class attributes are assigned to the <p> tag as intended.

Attribute Bindings | Output (9)



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.