Let’s Link Products With Category for our E-Commerce App
We will be creating a Vue.js application for displaying our Products and Categories.
Note
This tutorial has been depricated. Check the updated version here.
Table of Contents
- Introduction
- Requirements
- Setting up the Project
- Vue Components
- Vue Router
- Our Application
- Why use Vue Router
- Resources
Introduction
We are building a complete E-Commerce application.
So in this series of tutorials, we will now link our products with a category and design the user interface for it using Vue.js 3. In the past tutorial, we had designed the user interface for displaying products. Now, our products will have added information about their category.
If you are new to Vue.js, it is recommended to glance at the above article before moving on. So, our application will list all the products and categories and provide us with the following capabilities :
- Add a product
- Edit a product
- Add a category
- Edit a category
Requirements
In the last tutorial, we used the Vue CDN to create a single widget for displaying the products and injected that into our main HTML file. But now we will move a step ahead and build a complete Vue application, which is more powerful and robust. We will use the Vue CLI tool to set up our project. You also need to have Node.js installed before proceeding with the tutorial. We will not write any Node.js specific code but still, our Vue.js project requires Node. Apart from this, I will also be using Bootstrap, some Google Fonts and VS code as my editor. These things are up to your preferences.
Setting up the Project
The detailed instructions for setting up a Vue project can be found here, but let's quickly go through the process once. First, we will have to install the Vue CLI tool. It can be installed in the system with the following command:
npm install -g @vue/cliAfter installing the Vue CLI, the following command is used for creating a new project:
vue create project_nameNext, it will ask for some project configurations. Here is my project configuration:

After confirming all the details, we will have a project folder created for us by Vue CLI.

Folder Structure
Let’s go through the folder structure of our newly created Vue project
- public - contains the main HTML file of our project
- src/assets - stores the media files like images, logos, etc.
- src/components - stores all the reusable components of our project. These components are not unique to some specific route.
- src/router - contains the index.js file which keeps a record of all the routes
- src/views - stores all the router components. These are the components that are rendered as per the current route.
Apart from this, we have some important files too
- App.vue - it is the root component of our project
- main.js - it is the starting point of our project. Here we import our root component App.vue, our router file index.js and createApp method. After this, we mount our root component to the DOM using the following statement:
createApp(App).use(router).mount('#app');The folder structure is not enforced by Vue, we can customize this as per our requirements.
Vue Components
Almost all frontend frameworks allow us to create components that we can reuse at multiple places on the same or different websites. Some examples include a search bar, login form, product display component etc. In Vue, files with “.vue” extension are known as single file components. These single file components are composed of HTML, JavaScript, and CSS.
<template>
</template><script>
export default {}
</script><style></style>The template part contains the HTML of the component. The script tag contains the code defining the custom behavior of the component. And style tag houses the CSS of the component. As mentioned earlier, src/components and src/views contain all our components.
Vue Router
The Vue router enables us to link our browser URL and our components. This helps us define paths that get coupled with our components. This way Vue renders components based on the URL. The official page of Vue router lists all its features. The most important of all these is the ease of creating Single page applications (SPAs) using Vue router.
In Single page applications designed using Vue, the very first request sent to the server responds with a single HTML file and the JavaScript bundle. Then in the browser, Vue takes complete control over the application. After this, no further requests are sent to the server for new pages even if we change routes. This means once everything is loaded, navigating through different routes of our web application becomes very smooth. As an example, head over to Vue router guide and disconnect your internet once the page has loaded, even without the internet you will be able to use most of the features available there (even changing the guide language).
The src/router/index.js file contains information about all the router paths. Folder src/views contain the components which we couple with router paths. Because of their usage, these components are also known as router views.
In index.js file, we import all the router components and create an array of objects called routes. Objects of this array represent different routes of our application. Every route has a path, a name associated with it, and the component to be rendered for this path. For our project, I will be keeping the name property same as component name.
const routes = [
{
path: '/',
name: 'Home',
component: Home
}
]After creating this routes array, we use the createRouter method to create and export our router. Vue CLI is kind enough to do this task for us while creating our project.
Our Application
Now after understanding the Vue project structure and the requirement of different Vue features, we can dive into our E-Commerce project. For creating our application, we will be using many Vue components. So, there needs to be a structural hierarchy of components in our project. We can use the component tree to define this structure.

App.vue is the root component of our application. It is the first one to be rendered onto the DOM. Then we have our reusable components (in blue boxes) and router view components (in yellow boxes).
In App.vue, we have the data properties baseURL, products and categories. The baseURL refers to our backend API from where we fetch data and send requests for data addition and modification. The products and categories arrays, as their name suggests, stores our products and categories. We also have the components property which lists all the components which we use in the current component.
API call
In the mounted Lifecycle hook of our root component App.vue, we make the API call to fetch our products and categories. For this, we use JavaScript’s Fetch API.
const res = await fetch(this.baseURL + "product/");
this.products = await res.json();Fetching data from backend API is an asynchronous task. Hence the fetch method returns a promise. If our internet connection is stable and the server is up and running, the res variable will store the server response. Now to extract our data from this response, we use the json method. The json method reads the response stream, and since this stream could be very large, it takes time and returns a promise on completion. This promise once resolved returns a JavaScript object. In JavaScript, an array is also an object which can be represented in JSON.
The root component is the best place to fetch data from backend API because from here we can pass on this data as props to our child component. Let’s get this terminology clear. When we use a component inside of some other component, they are referred to as child and parent component respectively. A parent component can pass its data to a child component, this data is known as props. You can think of it as passing arguments to a function.
Router paths
In App.vue, we have used the components Navbar and Footer. Apart from this we also have a Vue tag <router-view>. This router-view tag renders component based on the URL path. Along with this, we also have the <router-link> tag. It acts as an anchor tag for a router-enabled app. The target location for this link is specified using the to prop. Let’s have a look at index.js file where we have defined all our routes.











