CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

31 December, 2020

Vue.js Data Binding

 Programing Coderfunda     December 31, 2020     Vue.js Tutorial     No comments   

Vue.js Data Binding

Vue.js supports different types of data binding. Before going to learn data binding in Vue.js, let's see what data binding is and how it is used.

What is Data binding?

Data binding is a technique used to bind data sources from the provider and consumer together and synchronize them at the time of retrieval. In a data binding process, whenever data is changed, it is reflected automatically by the elements bound to the data.

The term data binding is also used in cases where an outer representation of data in an element changes, and the underlying data is automatically updated to reflect this change.


Vue.js Class and Style Binding

In Vue.js data binding, we have to manipulate the class list and inline styles of an element. We already know that the class list and inline style are attributes so we can use v-bind to handle them. Vue.js provides special enhancements when we use v-bind with class and style. The expressions can also evaluate to objects or arrays along with strings.

Here, we will learn how to manipulate or assign values to HTML attributes, change the style, and assign classes with the help of Vue.js binding directive v-bind. Let's see a simple example to understand why and when to use v-bind directive for data binding.

Index.html file:

  1. <html>  
  2.    <head>  
  3.       <title>Vue.js Data Binding</title>  
  4.       <link rel="stylesheet" href="index.css">  
  5.         <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>  
  6.     </head>  
  7.     <body>  
  8.       <div id = "databinding">  
  9.          <h3>{{title}}</h3>  
  10.          <a href = "hreflink" target = "_blank"> Click Here </a> <br/>  
  11.          <a href = "{{hreflink}}" target = "_blank">Click Here </a>  <br/>  
  12.          <a v-bind:href = "hreflink" target = "_blank">Click Here </a>   <br/>  
  13.       </div>  
  14.       <script src="index.js"></script>  
  15.    </body>  
  16. </html>  

Index.js file:

  1. var vm = new Vue({  
  2.             el: '#databinding',  
  3.             data: {  
  4.                title : "Data Binding Simple Example",  
  5.                hreflink : "http://www.coderfunda.tk"  
  6.             }  
  7.          })  

Let's use a simple CSS file to make the output more attractive.

Index.css file:

  1. html, body {  
  2.     margin: 5px;  
  3.     padding: 0;  
  4. }  

After the execution of the program, you will see the following output:

Example Explanation

In the above example, you can see a title variable and three anchor links as following:

  1. <h3>{{title}}</h3>  
  2. <a href = "hreflink" target = "_blank"> Click Here </a> <br/>  
  3. <a href = "{{hreflink}}" target = "_blank">Click Here </a>  <br/>  
  4. <a v-bind:href = "hreflink" target = "_blank">Click Here </a>   <br/>  

We have also assigned a value hreflink : "http://www.coderfunda.tk" to the href from the data object.

  • If you click on the first Click Here link, it shows the href as hreflink so it will not redirect to the targeted url.
  • If you click on the second Click Here link, it shows the href as {{hreflink}}, so it will also not redirect to the targeted url.
  • Only the third one will redirect the correct targeted url because we have to bind it with the v-bind directive. See the following result.

So, to assign values to HTML attributes, we have to bind it with the v-bind directive as follows:

  1. <a v-bind:href = "hreflink" target = "_blank">Click Here</a>  

You can also use a shorthand to include v-bind directive as follows:

  1. <a :href = "hreflink" target = "_blank">Click Here</a>  

Note: If you inspect element of the result, you will see that the anchor tag does not show the v-bind attribute. It displays a plain HTML. The Vue.js properties are seen when we inspect the DOM.

Vue.js HTML Classes Binding

In Vue.js, we can bind an HTML class by using the v-bind: class directive. Let's see an example to demonstrate the binding of HTML classes in Vue.js.

Index.html file:

  1. <html>  
  2.    <head>  
  3.       <title>Vue.js Data Binding</title>  
  4.       <link rel="stylesheet" href="index.css">  
  5.         <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>  
  6.     </head>  
  7.     <body>  
  8.       <style>  
  9.          .active {  
  10.             background: skyblue;  
  11.          }  
  12.       </style>  
  13.       <div id = "classbinding">  
  14.          <div v-bind:class = "{active:isactive}"><b>{{title}}</b></div>  
  15.       </div>  
  16.       <script src="index.js"></script>  
  17.    </body>  
  18. </html>  

Index.js file:

  1. var vm = new Vue({  
  2.             el: '#classbinding',  
  3.             data: {  
  4.                title : "HTML class binding example",  
  5.                isactive : true  
  6.             }  
  7.          })  

After the execution of the program, you will see the following output:


In the above example, we have created a div with v-bind: class=" {active: isactive}". Here, isactive is a variable based on true or false values. This isactive variable is used to apply class active to the div. When we assign isactive variable as true, it shows the specified style in result as we have defined in the style .active with the background color as skyblue.

If you set the variable isactive is false, it will not apply the specified style in the result.

Index.js file:

  1. var vm = new Vue({  
  2.             el: '#classbinding',  
  3.             data: {  
  4.                title : "HTML class binding example",  
  5.                isactive : false  
  6.             }  
  7.          })  

After the execution of the program, you will see the following output:


Assign multiple classes to HTML tags

You can also assign multiple classes to HTML tags using v-bind attribute. See the following example:

Index.html file:

  1. <html>  
  2.    <head>  
  3.       <title>Vue.js Data Binding</title>  
  4.       <link rel="stylesheet" href="index.css">  
  5.         <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>  
  6.     </head>  
  7.     <body>  
  8.       <style>  
  9.          .info {  
  10.             color: #00529B;  
  11.             background-color: #BDE5F8;  
  12.          }  
  13.          div {  
  14.             margin: 10px 0;  
  15.             padding: 12px;  
  16.          }  
  17.          .active {  
  18.             color: #4F8A10;  
  19.             background-color: #DFF2BF;  
  20.          }  
  21.          .displayError{  
  22.             color: #D8000C;  
  23.             background-color: #FFBABA;  
  24.          }  
  25.       </style>  
  26.       <div id = "classbinding">  
  27.          <div class = "info"  v-bind:class = "{ active: isActive, 'displayError': hasError }">  
  28.             {{title}}  
  29.          </div>  
  30.       </div>  
  31.       <script src="index.js"></script>  
  32.    </body>  
  33. </html>  

Index.js file:

  1. var vm = new Vue({  
  2.             el: '#classbinding',  
  3.             data: {  
  4.                title : "HTML multiple class binding example",  
  5.                isActive : false,  
  6.                hasError : false  
  7.             }  
  8.          })  

In the above example, you can see that only the normal class is applied. Both other variables are false right now. Now, let' see what will happen when we set isActive variable to true:

  1. var vm = new Vue({  
  2.             el: '#classbinding',  
  3.             data: {  
  4.                title : "HTML multiple class binding example",  
  5.                isActive : true,  
  6.                hasError : false  
  7.             }  
  8.          })  

In the above example, we have assigned two classes to the div, info and active. Now, let's make hasError variable true and isActive false and then see the result.

  1. var vm = new Vue({  
  2.             el: '#classbinding',  
  3.             data: {  
  4.                title : "HTML multiple class binding example",  
  5.                isActive : false,  
  6.                hasError : true  
  7.             }  
  8.          })  

Now, in the above example, info and displayError class is applied to the div. This is how you can apply multiple classes according to the conditions.

Assign multiple classes using an array

We can also pass class as an array. See the following example to understand this well:

Syntax:

  1. <div v-bind:class="[activeClass, errorClass]"></div>  

Example:

Index.html file:

  1. <html>  
  2.    <head>  
  3.       <title>Vue.js Data Binding</title>  
  4.       <link rel="stylesheet" href="index.css">  
  5.         <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>  
  6.     </head>  
  7.     <body>  
  8.        <style>  
  9.          .info {  
  10.             color: #00529B;  
  11.             background-color: #BDE5F8;  
  12.          }  
  13.          div {  
  14.             margin: 10px 0;  
  15.             padding: 12px;  
  16.             font-size : 25px;  
  17.          }  
  18.          .active {  
  19.             color: #4F8A10;  
  20.             background-color: #DFF2BF;  
  21.          }  
  22.          .displayError{  
  23.             color: #D8000C;  
  24.             background-color: #FFBABA;  
  25.          }  
  26.       </style>  
  27.       <div id = "classbinding">  
  28.          <div v-bind:class = "[infoclass, errorclass]">{{title}}</div>  
  29.       </div>  
  30.       <script src="index.js"></script>  
  31.    </body>  
  32. </html>  

Index.js file:

  1. var vm = new Vue({  
  2.             el: '#classbinding',  
  3.             data: {  
  4.                title : "Multiple classes with array example",  
  5.                infoclass : 'info',  
  6.                errorclass : 'displayError'  
  7.             }  
  8.          })  

After the execution of the program, you will see the following output:

In the above example, both classes are applied to the div.

Vue.js Inline Style Binding

Object Syntax

The object syntax for v-bind:style is just like CSS, but it's a JavaScript object. You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names:

HTML:

  1. <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>  

CSS:

  1. data: {  
  2.   activeColor: 'red',  
  3.   fontSize: 30  
  4. }   

You can also bind a style object directly to make the template cleaner.

HTML:

  1. <div v-bind:style="styleObject"></div>  

CSS:

  1. data: {  
  2.   styleObject: {  
  3.     color: 'red',  
  4.     fontSize: '30px'  
  5.   }  
  6. }  

Let's take an example to understand the concept clearly.

Index.html file:

  1. <html>  
  2.    <head>  
  3.       <title>Vue.js Data Binding</title>  
  4.       <link rel="stylesheet" href="index.css">  
  5.         <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>  
  6.     </head>  
  7.     <body>  
  8.       <div id = "databinding">  
  9.          <div v-bind:style = "{ color: activeColor, fontSize: fontSize + 'px' }">{{title}}</div>  
  10.       </div>  
  11.       <script src="index.js"></script>  
  12.    </body>  
  13. </html>  

Index.js file:

  1. var vm = new Vue({  
  2.             el: '#databinding',  
  3.             data: {  
  4.                title : "Inline style Binding Example",  
  5.                activeColor: 'red',  
  6.                fontSize :'30'  
  7.             }  
  8.          })  

After the execution of the program, you will see the following output

In the above example, you can see that the style is applied for the div, and the data is fetched from the data object.

You can fetch the same result by assigning all the values to a variable and then assigning the variable to the div.

Example:

Index.html file:

  1. <html>  
  2.    <head>  
  3.       <title>Vue.js Data Binding</title>  
  4.       <link rel="stylesheet" href="index.css">  
  5.         <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>  
  6.     </head>  
  7.     <body>  
  8.       <div id = "databinding">  
  9.          <div v-bind:style = "styleobj">{{title}}</div>  
  10.       </div>  
  11.       <script src="index.js"></script>  
  12.    </body>  
  13. </html>  

Index.js file:

  1. var vm = new Vue({  
  2.             el: '#databinding',  
  3.             data: {  
  4.                title : "Inline style Binding Example-2",  
  5.                styleobj : {  
  6.                   color: 'red',  
  7.                   fontSize :'30px'  
  8.                }  
  9.             }  
  10.          })  

In the above example, you can see that the color and the fontSize is assigned to the object called styleobj and the same is assigned to the div.

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • VueJS - Reactive InterfaceVueJS provides options to add reactivity to properties, which are added dynamically. Consider that we have already created vue instance and need to ad… Read More
  • VueJS RoutingVueJS does not have a built-in router feauture. We need to follow some additional steps to install it.Direct Download from CDNThe latest version of vu… Read More
  • VueJS Directives Directives are instruction for VueJS to do things in a certain way. We have already seen directives such as v-if, v-show, v-else, v-for, v-bind … Read More
  • VueJS MixinsMixins are basically to be used with components. They share reusable code among components. When a component uses mixin, all options of mixin become a… Read More
  • VueJS Render Function We have seen components and the usage of it. For example, we have a content that needs to be reused across the project. We can convert the same … Read More
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Write API Integrations in Laravel and PHP Projects with Saloon
    Write API Integrations in Laravel and PHP Projects with Saloon Saloon  is a Laravel/PHP package that allows you to write your API integratio...
  • Features CodeIgniter
    Features CodeIgniter There is a great demand for the CodeIgniter framework in PHP developers because of its features and multiple advan...
  • Laravel Breeze with PrimeVue v4
    This is an follow up to my previous post about a "starter kit" I created with Laravel and PrimeVue components. The project has b...
  • Credit card validation in laravel
      Validation rules for credit card using laravel-validation-rules/credit-card package in laravel Install package laravel-validation-rules/cr...
  • Fast Excel Package for Laravel
      Fast Excel is a Laravel package for importing and exporting spreadsheets. It provides an elegant wrapper around Spout —a PHP package to ...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (68)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh - 9/21/2024
  • pyspark XPath Query Returns Lists Omitting Missing Values Instead of Including None - 9/20/2024
  • SQL REPL from within Python/Sqlalchemy/Psychopg2 - 9/20/2024
  • MySql Explain with Tobias Petry - 9/20/2024
  • How to combine information from different devices into one common abstract virtual disk? [closed] - 9/20/2024

Laravel News

  • Lightning Fast Schedule Management for Laravel - 6/20/2025
  • Reset Rate Limits Dynamically with Laravel's clear Method - 6/18/2025
  • Manipulate Image URLs in Laravel with the Image Transform Package - 6/19/2025
  • Handle Nested Arrays Elegantly with Laravel's fluent() Helper - 6/18/2025
  • Laravel 12.19 Adds a useEloquentBuilder Attribute, a FailOnException Queue Middleware, and More - 6/18/2025

Copyright © 2025 CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda