VueJS

Building Reusable Components in Vue 3

Mark Shenouda
4 min read
Building Reusable Components in Vue 3
This article was written over 18 months ago and may contain information that is out of date. Some content may still be relevant, but please refer to official documentation for the latest information.

What is Vue

Vue.js is an open-source model–view–viewmodel front end JavaScript framework for building user interfaces and single-page applications. It was created by Evan You. From its official documentation “Vue is a JavaScript framework for building user interfaces. It builds on top of standard HTML, CSS and JavaScript, and provides a declarative and component-based programming model that helps you efficiently develop user interfaces, be it simple or complex.”

Components basics

The way we build apps and websites in Vue is different than using pure HTML, CSS, and JavaScript. In Vue, the app is like a tree of components.

Two ways to write Vue components

There are two ways to write Vue components: the old (classic) way, by using Vue class component, and the new (modern) way by using script setup and composition APIs.

File structure in both ways

Vue follows the same file structure in both ways. First, each component should be in a separate file with the .vue extension, and each Vue file contains three sections.

<template>

We write the HTML template here.

</template>

<script>

// We write the JavaScript logic here.

</script>

<style>

/* We write the CSS styles for the component here. */

</style>

Both ways share the same file structure. The only difference is in the <script> section.

In the Vue class components way

The <script> section should return the Vue object like this:

<script>
export default {
	name: “component-name”,
	props: {
		// …
	},
	data() {
		// …
},

// …

}
</script>

Then we use script setup:

<script setup>

// We put our component’s JavaScript code here:

</script>

The new way is more minimal, and we access all the Vue component class features using composition APIs (it’s more like React hooks for React if you’re familiar with React).

Component Registration

Each component you create in your Vue app should be registered so Vue knows where to locate its implementation when it is encountered in a template.

To register your component in another component, you should add it to the components property in the Vue object.

<template>
 	<AnotherComponent />
</template>

<script>
import AnotherComponent from './AnotherComponent.vue'

export default {
  components: {
    AnotherComponent
  }
}
</script>

In the script setup way, you don’t need to manually register components. They are being registered automatically when you import them:

<script setup>
import AnotherComponent from ‘./AnotherComponent.vue’;
</script>

<template>
	<AnotherComponent />
</template>

Props

Props allow developers to pass data from the parent component to children components. Vue components require explicit props declarations so that Vue knows what external props passed to the component should be treated as fall through attributes.

<AnotherComponent message=”Hello world!” />

To declare props in your component, you should add them in the props property in the Vue object:

<script>
export default {
  props: {
    title: String,
    likes: Number
  }
}

</script>

For each property in the object declaration syntax, the key is the name of the prop, while the value should be the constructor function of the expected type.

Note: it’s important to declare each Prop’s type too to avoid errors in production.

In script setup, you define props with defineProps() from composition APIs:

<script setup>
const props = defineProps({
	title: String,
	likes: Number
})
</script>

Now let’s build a todo app

The best way to practice these concepts is by building a small app.

First, let’s initialize a new project using Vite:

npm init vite@latest

Or you can use Stackblitz like me in this tutorial. Go to the Vite tab and choose Vue:

image

Go to App.js, remove its content, and add the title.

image

Let's create the List component where we will show the user all of the todos.

<script setup>
defineProps({
  todos: Array,
});
</script>

<template>
  <ul>
    <li v-for="todo in todos">
      {{ todo }}
    </li>
  </ul>
</template>

As you see, it's a very simple component. We just pass the todos array as a prop, and then use v-for to print it.

Then, import the list component so we can use it in App.js.

<script setup>
import List from './components/List.vue';
</script>

<template>
  <h1>Todo List</h1>
  <List />
</template>

Now, let's create the form to add todos from.

<template>
  <h1>Todo List</h1>
  <List />

  <form>
    <input type="text" placeholder="Write a new task" />
    <button type="submit">Add</button>
  </form>
</template>

We will now need to add the states. In the new approch, we use ref().

<script setup>
  import { ref } from 'vue';

  const todos = ref([]);
  const newTodo = ref("");
</script
  • todos is where we will store all the todos
  • newTodo is to store the current form value

Then, we need to bind these states with the <List /> component, and the form input.

<List :todos="todos" />
<input type="text" placeholder="Write a new task" v-model="newTodo" />

Finally, we need to create the submit function to add a new todo on submit, and clear the form for a new one.

function handleSubmit() {
  todos.value.push(newTodo);
  newTodo.value = '';
}

Then, bind this function with the form.

<form @submit.prevent="handleSubmit">

Here is the complete project for you to check out and try yourself!

If you need additional assistance, feel free to reach out to me on Twitter!

About the author

Mark Shenouda

Mark Shenouda

Software Engineer

Keep reading

View all posts →

Awesome 3D experience with VueJS and TresJS: a beginner's guide

Unleash the power of 3D in your Vue.js projects with Tres.js! The future of immersive web experiences is here. #Vuejs #3Ddevelopment...

Mattia Magi5 mins
Vue3D

3 VueJS Component Libraries Perfect for Beginners

For developers checking out VueJS for the first time, the initial steps are overwhelming, particularly when setting up projects from square one. But don’t worry! The VueJS ecosystem offers a plethora of remarkable component libraries, easing this early obstacle. These three libraries are pre built toolkits, providing beginners with the means to kickstart their VueJS projects effortlessly. Let’s take a look! Quasar Quasar is among the most popular open source component libraries for Vue.js, offering a comprehensive set of ready to use UI components and tools for building responsive web applications and websites. Designed with performance, flexibility, and ease of use in mind, Quasar provides developers with a wide range of customizable components, such as buttons, forms, dialogs, and layouts, along with built in support for themes, internationalization, and accessibility. With its extensive documentation, active community support, and seamless integration with Vue CLI and Vuex, Quasar empowers developers to rapidly prototype and develop high quality Vue.js applications for various platforms, including desktop, mobile, and PWA (Progressive Web Apps). PrimeVue PrimeVue is a popular Vue.js component library offering a wide range of customizable UI components designed for modern web applications. Developed by PrimeTek, it follows Material Design guidelines, ensuring responsiveness and accessibility across devices. With features like theming, internationalization, and advanced functionalities such as lazy loading and drag and drop, PrimeVue provides developers with the tools to create elegant and high performing Vue.js applications efficiently. Supported by clear documentation, demos, and an active community, PrimeVue is an excellent choice for developers seeking to streamline their development process and deliver polished user experiences. Vuetify Vuetify is a powerful Vue.js component library that empowers developers to create elegant and responsive user interfaces with ease. Built according to Google's Material Design guidelines, Vuetify offers a vast collection of customizable UI components, ranging from buttons and cards to navigation bars and data tables. Its comprehensive set of features includes themes, typography, layout grids, and advanced components like dialogues and sliders, enabling developers to quickly build modern web applications that look and feel polished. With extensive documentation, active community support, and ongoing development, Vuetify remains a top choice for Vue.js developers seeking to streamline their workflow and deliver visually stunning user experiences. For newcomers venturing into Vue.js, the initial setup might seem daunting. Thankfully, Vue.js offers a variety of component libraries to simplify this process. Quasar, PrimeVue, and Vuetify are standout options, each providing pre built tools to kickstart projects smoothly. Whether you prefer Quasar's extensive UI components, PrimeVue's Material Design inspired features, or Vuetify's responsive interfaces, these libraries cater to diverse preferences and project requirements. With their clear documentation and active communities, these libraries empower developers to start Vue.js projects confidently and efficiently, enabling Vue developers to create polished user experiences....

2 mins
VuetifyVueQuasar

Understanding Vue.js's <Suspense> and Async Components

In this blog post, we will delve into how <Suspense> and async components work, their benefits, and practical implementation strategies to make your Vue.js applications more efficient and user-friendly...

Jesús Padrón6 mins
VueWeb PerformanceUXJavaScript

Getting started with Vitepress

Create your static blog site using Vitepress. Setup and configure your site in minutes by following this step-by-step tutorial....

Simone Cuomo12 mins
VueViteVitepress