Creating Dynamic Tabs in Vue.js with Server Data and the "KeepAlive" Component
Tags: vue, nuxt, javascript, tutorial
Published: 2023-10-26 09:52:32


![Creating Dynamic Tabs in Vue.js](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1b80oo7j2e32pboosyd.jpg)

> Check [this post](https://webcraft-notes.com/blog/creating-dynamic-tabs-in-vuejs-with-server) in my [web notes](https://webcraft-notes.com/)

Introduction:

In our previous post, '[Vue.js: Build Simple Tabs with Dynamic Components](https://webcraft-notes.com/blog/vuejs--build-simple-tabs-with-dynamic)' we explored creating basic tabs with dynamic components. But what if we need to fetch data from the server for each tab? What if this data needs to be updated selectively upon tab activation? Join me in this post as we delve into the intricacies of fetching server data for dynamic tabs and explore the functionality of the 'KeepAlive' component. Let's unravel the complexities together...

We will not dive deep into how to start a new project, or how to build tabs, as I mentioned you can find it in my previous post '[Vue.js: Build Simple Tabs with Dynamic Components](https://webcraft-notes.com/blog/vuejs--build-simple-tabs-with-dynamic)'. Here we will start from already existing tabs and step 4.

Step 4: Fetch and render data.

We will use Free Fake API for testing 'JSON Placeholder'. There is a cool endpoint for us 'https://jsonplaceholder.typicode.com/photos' that returns a list of images with descriptions, but also we can add an ID and receive only one image. 
In our Tab.vue component in the created hook, let's generate a random integer number to fetch different images every time our tab was created, then asynchronously fetch the photo and store it in the 'photo' variable. After the photo is successfully received it will be rendered in our Tab component. We will add this functionality to all our Tabs, and every time the active Tab changes new image will be shown. Here is the Tab.vue code example:

![Tabs component](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ykr5tood108l2jmasjxu.png)

Let's start our project 'npm run serve' and check the result:

![First tab page result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2lzxyearr0o94ahbci4v.png)

![Second tabresult](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7xp1t3a5zhl5i2vqfs7d.png)

It looks great, our tabs work correctly. Also, we can check fetched data in Vue.js dev tools in the browser window.

![Vue.js devtools](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdg3pt43zlqk8w7vtiiu.png)

Return a few times to the same tab and the image will be updated. It's useful if we update data in the server often, and need to fetch and rerender it in our app. But what if we need to receive it once? Or what if we care about our server resources? Not a big deal, because we have a <KeepAlive> component.

Step 5. <KeepAlive> component.

Okay, let's wrap out the dynamic component into the <KeepAlive> component and check what changed.

![KeepAlive component](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v3xoo5ot7522s9k43gxk.png)

![KeepAlive result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kq677oa6d90sjwytv6vx.png)

Everything works fine, but when you change the tab and return to the previous it will not send the request to the server for data, because thankfully to <KeepAlive> component tab was not destroyed and it still stores data. You can check it in Dev tools, tabs still exist but some of them are just inactive.

[Continue reading...](https://webcraft-notes.com/blog/creating-dynamic-tabs-in-vuejs-with-server)