Lazy loading implemented with BootstrapVue's b-nav component

Having some trouble wrapping my head around the following issue:

I've created a Vue.js component with tabs that have routes. I opted for a variation of the b-nav Tabs style (official docs) and it's functioning well in terms of tabs and routing.
The challenge lies in figuring out how to lazy load the tab-content for each item in myItems instead of loading all at once when one of the tab routes is requested.

The routes are structured like this: localhost/items/#tab0, localhost/elements/#tab1, and so on.

(By the way, b-tabs support built-in lazy loading but don't support routing! Hence, they can't be utilized :-/)

Here's the template code of my component:

<template>
    <div class="tabs">
        <b-nav tabs>
            <b-nav-item
                v-for="(item, index) in myItems"
                :key="item.Id"
                :to="'#tab' + item.Id"
                :active="$route.hash === '#tab' + item.Id || (index === 0 && $route.hash === '')"
            >
                {{ item.name }}
            </b-nav-item>
        </b-nav>

        <div class="tab-content">
            <div
                v-for="(item, index) in myItems"
                :key="item.Id"
                class="tab-pane"
                :class="{ active: $route.hash === '#tab' + item.Id || (index === 0 && $route.hash === '') }"
            >
                <!-- individual output here, depending on route | I want to lazy load this -->
            </div>
        </div>
    </div>
</template>

And here's the TypeScript code:

<script lang="ts">
    import { Vue, Component, Prop } from "vue-property-decorator";

    @Component()
    export default class MyItemsTabs extends Vue {
        @Prop() readonly myItems!: Item[] | null;
    }
</script>

UPDATE: Out of 7 tabs, I only need to lazy load 2, while the rest should load immediately.

Any suggestions or ideas on how to achieve this? Appreciate your help in advance :-)

Answer №1

Here is my approach to solving the issue:

To manage the tab content, I developed a component specifically for this purpose:

This is the template structure I used:

<div class="tab-content">
  <TabContent
    v-for="(item, index) in myItems"
    class="tab-pane"
    :class="{ active: $route.hash === '#tab' + item.Id || (index === 0 && $route.hash === '') }"
    :key="item.Id"
    :tab-content="item"
  />
</div>

Within the TabContent component, I included the following logic:

This is the template setup:

<template>
    <div>
        <!-- Display TabContent1 without pre-loading if conditions are met -->
        <TabContent1
            :v-if="$route.hash === "#tab" + item.Id && item.name === 'tabName1'"
        />

        <!-- Display TabContent2 without pre-loading if conditions are met -->
        <TabContent2
            :v-if="$route.hash === "#tab" + item.Id && item.name === 'tabName2'"
        />

        <!-- Load default content immediately if other conditions are not met -->
        <TabContentDefault v-else />
    </div>
</template>

Please note, the code provided is simplified and in actual practice, I incorporate functions and additional properties for more comprehensive functionality.

Answer №2

There are a couple of approaches to consider in this situation:

One option is to utilize b-tab and set up routing for them, leveraging query parameters for assistance. It's important to incorporate @change each time a tab is switched.

Alternatively, you could create a dedicated component for displaying your output:

<MyCustomComponent v-for="(item, index) in myItems" :key="item.id" :is="selected_component_name " />

Refer to the vue documentation for further clarification.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Extract objects from a nested array using a specific identifier

In order to obtain data from a nested array of objects using a specific ID, I am facing challenges. My goal is to retrieve this data so that I can utilize it in Angular Gridster 2. Although I have attempted using array.filter, I have struggled to achieve t ...

How do you transfer byte[] data using a DTO in Java Spring?

I am currently facing an issue with my server-side application. The problem arises when attempting to convert a Blob to an Excel file on the front-end, specifically when the byte[] is sent within a DTO. When sending a POST request from the back-end (sprin ...

How to show an image in a Vue component using a Laravel storage folder

My images are being stored in the following location: Storage/app/public/userImages They are saved successfully, but when I try to retrieve them in my Vue component, I get a 404 error stating that they are not found. <img :src="`/storage/${post. ...

Tips and tricks for personalizing an npm package in vue.js

I've been working on customizing a multiselect package to incorporate tab events in vuejs, but so far I haven't seen any changes reflected. I'm looking for guidance on how to modify a library within Vue. My approach was to navigate to the n ...

The autocomplete feature in Atom is not functioning as expected

Autocomplete+ is included with the installation of Atom and is activated by default. I have noticed that when I am coding, no suggestions are appearing. What could be causing this issue? Do I need to adjust any files in order for Autocomplete+ to functio ...

Can't get className to work in VueJS function

I need help changing the classNames of elements with the "link" class. When I call a method via a click action, I can successfully get the length of the elements, but adding a class does not seem to work. Does anyone have any insights into this issue? HTM ...

Unable to load component - utilizing vue-airbnb-style-datepicker

Attempting to use the vue-airbnb-style-datepicker Added to my file.vue: import AirbnbStyleDatepicker from 'vue-airbnb-style-datepicker' import format from 'date-fns/format' export default { components: { AirbnbStyleDatepicker ...

Bringing in information from a TypeScript document

I'm having trouble importing an object from one TypeScript file to another. Here is the code I am working with: import mongoose from "mongoose"; import Note from './models/notes'; import User from './models/users'; import ...

How can I utilize a personalized SSL certificate in Nuxt during development mode?

I need to have HTTPS on localhost for a dependency I am using. To achieve this, I added the following code in my nuxt.config.js file: server: { https: { key: fs.readFileSync(path.resolve(__dirname, 'localhost-key.pem') ...

Associate the generic operator <T> with a FunctionConstructor

In my TS function, I deserialize JSON into a specific type/object by providing it with a constructor function of that type which reconstructs the object from JSON. Here is how it looks like: export function deserializeJSON<T>(JSONString: string, ty ...

What are some ways to customize the appearance of a Vue stacked b-table?

I recently utilized a stacked b-table in bootstrap-vue. <b-modal :id="infoModal.id" :title="infoModal.title" hide-footer @hide="resetInfoModal;"> <pre> <b-table stacked sticky-header head- ...

What makes FC function components stand out from traditional vanilla React function components?

I recently came across this FC function component: const LabelForm: FC<LabelFormProps> = ({ labels, selectedID, }) => { const selectedLabel = selectedID !== undefined && labels[selectedID]; In my usual implementation, I do it like t ...

What is the best way to send a JSON object in Vue.js?

<template> <div id="app"> <div id="bee-plugin-container"></div> </div> </template> <script> // import axios from "axios"; // import Bee from "@mailupinc/bee-plugin"; import $ from 'jquery' ...

Encountering a warning message in Vue 3 Migration Build using Typescript: Error in finding export 'default' (imported as 'Vue') within the 'vue' package

I've been working on migrating my vue2 application to Vue3 using the Migration Build. I diligently followed the steps outlined in the documentation available at https://v3-migration.vuejs.org/migration-build.html, but despite that, I still keep encoun ...

Faking a distant site to access a nearby document

I am currently developing a Vue JS front-end that communicates with a C# webapi application. The webapi is designed to store images in a folder located on the E: drive of the server and provide src links for the UI to use. While everything works as expect ...

Querying data conditionally with Angular rxjs

I have a json file that contains multiple arrays structured like this: { A[] B[] C[] ... } This is the query I am using: myFunction():void{ this.apiService.getData() .pipe( map((response: any) => response.A), // to access to the &ap ...

Creating a personalized menu using Nextron (electron) - Step by step guide

I'm currently in the process of developing an app with Nextron (Electron with Nextjs and Typescript). Although I have the foundation of my Next app set up, I've been encountering issues when attempting to create a custom electron menu bar. Every ...

"Vue i18n encountered an issue while attempting to retrieve translations from the locales

I have integrated i18n into my Vue project using the command vue add i18n However, after adding i18n, it seems unable to load from the json files located in the folder locales The issue specifically arises when trying to access the message key in the Hel ...

What is the process for implementing a component in antdesign when using vue-cli and vue 3?

I followed the instructions provided in the documentation here. These are the steps I took: vue create example-app cd example-app I selected the vue 3 preset. Then, I made changes to the script in main.js import Vue from 'vue'; import Button f ...

Considering the switch to Vue 3?

Currently working on a Vue 2 project, I recently discovered that Vue 3 was released in September. I'm now eager to explore the process of upgrading from Vue 2 to Vue 3. Are there any specific guides available for this migration? In case there isn&apos ...