Data Binding In Vue.js

The connection between app UI and the data display its call data bind. Whenever data change it is reflected automatically by the elements bound to the data. This process known as data binding process.

In this blog, we will learn how to data bind vue.js.

Step 1: Create a new vue project:

  • vue create example

Step 2: Install bootstrap :

  • npm install bootstrap

Step 3: open main.js file and import bootstrap path :

import Vue from 'vue'
import App from './App.vue'
import router from '../src/router/index.js'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'

Vue.config.productionTip = false

new Vue({
  render: h => h(App),

Step 4: open App.vue file and add following lines :

  <section class="container">
      <h2>Data Binding & v-if</h2>
      <input type="text" v-on:input="setMessage($" v-on:keyup.enter="confirmMessage" />
      <span>Message - {{ message }}</span>
      <button type="button" class="btn btn-dark" @click="sendMessage()">ok</button>
      <span v-if="!showMsg">Send Messae Here</span>
      <span v-if="showMsg">{{ confirmMessage }}</span>

export default {
  name: 'App',
  data() {
    return {
      message: '',
      confirmMessage: '',
      showMsg: false
  methods: {
    setMessage($event) {
      this.message = $event
    sendMessage() {
      if (this.message == '') {
        this.showMsg = false
        this.confirmMessage = 'Send Messae Here'
      } else {
        this.showMsg = true
        this.confirmMessage = this.message

Code in Action:

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories