Sunday, March 3, 2024     Bojan Arsenovic     Web Dev Technologies

Featured Photo

In the ever-evolving world of web development, efficiency and consistency are the keystones of a robust application. One of the common hurdles developers face is code duplication, which not only bloats the project but also creates a nightmare when it comes to maintenance and updates. Vue 3, with its intuitive API and component-based architecture, presents a powerful solution to this problem through reusable components. Let's dive into how reusable components, exemplified by an AddressForm component, can significantly streamline your Vue 3 projects.

The Need for Reusable Components

The allure of reusable components lies in their ability to encapsulate functionality in a self-contained unit that can be easily integrated across your application or even across projects. Here's why they are indispensable:

  • Code Efficiency: By centralizing code logic and markup, reusable components eliminate the need to write duplicate code, making your codebase cleaner and more manageable.
  • Consistency: They ensure that elements like forms and buttons look and behave consistently throughout your application, providing a seamless user experience.
  • Faster Development: Components can be developed, tested, and debugged in isolation, then reused, speeding up the development process.

Why Vue 3 is Ideal for Reusable Components

Vue 3 supports the Options API, which continues to be a familiar and favored way for many developers to define their components. This API allows you to structure your component options, such as data, methods, and computed properties, in a straightforward and organized manner. It's this clarity and simplicity that make Vue 3 an ideal choice for crafting reusable components, alongside its improved reactivity system which ensures that the UI stays up-to-date with the application state.

Creating a Reusable AddressForm Component

Our example, the AddressForm component, demonstrates how to create a form with typical address fields such as street, city, postal code, province and country. Given that we're styling our components with Bootstrap 5.3, we can focus more on functionality and less on custom CSS, leveraging Bootstrap's form classes for design.

Component Structure

Our form's template uses v-model for two-way data binding, ensuring that the component's state is automatically updated with the user's input.

Script and Props Using Options API

<template>
  <div>
    <div class="mb-3">
      <label for="address" class="form-label">Address</label>
      <input type="text" v-model="innerAddress" class="form-control" @input="$emit('update:address', innerAddress)">
    </div>
    <div class="mb-3">
      <label for="city" class="form-label">City</label>
      <input type="text" v-model="innerCity" class="form-control" @input="$emit('update:city', innerCity)">
    </div>
    <div class="mb-3">
      <label for="province" class="form-label">Province</label>
      <input type="text" v-model="innerProvince" class="form-control" @input="$emit('update:province', innerProvince)">
    </div>
    <div class="mb-3">
      <label for="postalCode" class="form-label">Postal Code</label>
      <input type="text" v-model="innerPostalCode" class="form-control" @input="$emit('update:postalCode', innerPostalCode)">
    </div>
    <div class="mb-3">
      <label for="country" class="form-label">Country</label>
      <select v-model="innerCountry" class="form-select" @change="$emit('update:country', innerCountry)">
        <option value="">Please select one</option>
        <option>Canada</option>
        <option>United States</option>
      </select>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AddressForm',
  props: {
    address: String,
    city: String,
    province: String,
    postalCode: String,
    country: String
  },
  data() {
    return {
      innerAddress: this.address,
      innerCity: this.city,
      innerProvince: this.province,
      innerPostalCode: this.postalCode,
      innerCountry: this.country
    };
  },
  watch: {
    address(newVal) {
      this.innerAddress = newVal;
    },
    city(newVal) {
      this.innerCity = newVal;
    },
    province(newVal) {
      this.innerProvince = newVal;
    },
    postalCode(newVal) {
      this.innerPostalCode = newVal;
    },
    country(newVal) {
      this.innerCountry = newVal;
    }
  },
  emits: ['update:address', 'update:city', 'update:province', 'update:postalCode', 'update:country']
};
</script>

In this example, the AddressForm component is defined using Vue 3's Options API, focusing on utilizing props for initial values and data for maintaining the state. This structure allows for two-way data binding by emitting an event for the parent component to handle updates, keeping the form fields and the parent component's data in sync.

Utilizing the AddressForm in a Vue 3 Application

Our AddressForm component is designed to be versatile, easily integrated into any part of the application, such as collecting a customer's billing and shipping addresses in an order form.

<template>
  <section>
    <h2>Billing Address</h2>
    <AddressForm
      v-model:address="billingAddress.address"
      v-model:city="billingAddress.city"
      v-model:province="billingAddress.province"
      v-model:postalCode="billingAddress.postalCode"
      v-model:country="billingAddress.country"
    />
    <h2>Shipping Address</h2>
    <AddressForm
      v-model:address="shippingAddress.address"
      v-model:city="shippingAddress.city"
      v-model:province="shippingAddress.province"
      v-model:postalCode="shippingAddress.postalCode"
      v-model:country="shippingAddress.country"
    />
  </section>
</template>

<script>
import AddressForm from './components/AddressForm.vue';

export default {
  components: {
    AddressForm
  },
  data() {
    return {
      billingAddress: {
        address: '',
        city: '',
        province: '',
        postalCode: '',
        country: ''
      },
      shippingAddress: {
        address: '',
        city: '',
        province: '',
        postalCode: '',
        country: ''
      }
    };
  }
}
</script>

Conclusion

The AddressForm component exemplifies the power of reusable components in Vue 3, showing how developers can create efficient, consistent, and maintainable web applications. By leveraging Vue 3's Options API and Bootstrap for styling, we can build components that are not only functional but also aesthetically pleasing with minimal effort.

Eager to enhance your web applications with reusable Vue 3 components or looking for expert guidance on your Vue 3 project? Our team specializes in crafting high-quality, custom solutions tailored to your unique needs. Contact i2b Global today to discover how we can help you achieve your project goals and elevate your online presence.


  Go Back



Google Rating

4.6      9 reviews


Photo of D R

D R
  July 20, 2023

We have been using I2B Global for over 5 years and for multiple business ventures, and we could not be more pleased with the service we have received. Bob and his team have been incredibly accommodating, supportive, and always share their wealth of experience. I could not recommend I2B Global more, Thanks for all your work.

Photo of Ramon P. Schuler

Ramon P. Schuler
  February 19, 2022

AMAZING COMPANY WITH FOLKS WHO CARE!! RPS

Photo of Ace Luxury

Ace Luxury
  August 22, 2021

To Bob, Bojan, and the I2B Global Team: Thank you so much for the outstanding work you have done for us so far. Your way of responding to our needs is truly a breath of fresh air in this fast paced era we live in. We continue to add more services your firm has to offer given how effective your site design and SEO has been. We look forward to continued growth along with you for years to come. Keep up the excellent work.

Photo of Grant McGuinty

Grant McGuinty
  March 19, 2021

As a neophyte in the software business I cannot express enough how happy I am to deal with Bob Gill at i2b Global Inc. The company is with me every step of the way. Kind, professional and very responsive are the best words to describe them. I look forward to grow with them in the future with my FinalDocx by Executor Choice distribution business.

Photo of Al Mickeloff

Al Mickeloff
  February 12, 2017

We have been a client of I2b Global Inc. since 2007. While they are a smaller company, they have the knowledge, experience and responsiveness of a much larger firm and they are up-to-date with the latest online improvements and trends. Similar to other web development companies, they can build you a nice website but where they excel is at the customizations needed for your business and most importantly delivering these changes at a reasonable price with expert guidance and advice. Any support issues or change requests are dealt with very quickly and it is not uncommon to see this happen even in the evenings and weekends. If you need a professional website and a reliable company to support and host it we highly recommend I2b Global Inc. Al Mickeloff, Marketing Manager – Canadian Warplane Heritage Museum

View All Google Reviews


 Request a Quote