March 3, 2024     Bojan Arsenovic     Web Development

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.7      12 reviews


Photo of Kirsten McNamee

Kirsten McNamee
  February 14, 2026

I’m extremely impressed with the team who created my website. They demonstrated exceptional precision and architectural design, delivering a site that is both compliant and visually engaging. Every question I’ve asked has been met with professional, direct responses — and their timing and attention to detail is outstanding. They even created an incredible logo that fits my brand perfectly. I feel confident about the site’s current SEO and AI recognition, and their professional advice, every step of the way, has given me clarity and trust moving forward. They managed every aspect specifically, precisely, and with care — everything I value in a website, period. Highly recommended. Kudos Bob et al and thank you all.

Photo of Austin Hancock

Austin Hancock
  February 12, 2026

Simply the best! Bob and Bojan are a pleasure to work with. They are both great with communication, and get things done quickly - but not at the cost of accuracy. Our museum highly recommends i2B Global, as they helped bring our website into the 21st Century. Thanks, guys!

Photo of Sam

Sam
  February 08, 2025

Bob at i2b global has been fantastic to deal with. I've been using this firm since 1995 and they have never let me down. I believe they are to notch and always perform beyond our expectations. I would highly recommend Bob and his team to all my family and friends. GREAT WORK!!!

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 Paul Schuler

Ramon Paul Schuler
  February 19, 2022

AMAZING COMPANY WITH FOLKS WHO CARE!! RPS

View All Google Reviews


 Request a Quote