Load components dynamically based on list of identifiers

With over 100 components at my disposal, not all of them will be needed on a single page.
For instance, a user might select a list of component IDs (components_ids - [7, 50, 22]).
When the page is visited, I will only load the components with IDs that match 7, 50, or 22 from the components_ids list.

I have realized that all components can be loaded on the page and used dynamically using :is, but loading all 100+ components consumes unnecessary memory resources.

Is there a way for me to load only the components that are selected by the user?

Any suggestions would be greatly appreciated!

Answer №1

Check out this link for information on dynamic async components in Vue.js: https://v2.vuejs.org/v2/guide/components-dynamic-async.html#Async-Components

<template>
  <div>
    <component v-for="component in myComponents" :is="component"/>
  </div>
</template>

<script>
export default {
  data() {
    return {
      component_names: ['select', 'input', 'modal']
    }
  },
  computed: {
    myComponents() {
      return this.component_names.map(name => () => import(`@/components/${name}.vue`));
    }
  }
}
</html>

I used descriptive names instead of numeric IDs to demonstrate mapping from IDs to component paths.

The use of double lambda functions might seem daunting at first glance, but it's a way to map strings to functions that load components dynamically.

<component :is="() => import('@/components/hello.vue')"
is an example of how to dynamically load components.

However, using

:is="import('@/components/hello.vue')"
directly won't achieve the same result.

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

Should I use a single directive with ng-if statements in AngularJS, or opt for two separate directives?

Question about optimizing performance in Angular JS. I am using ng-repeat to display a list of search results. Each search item is extracted using a directive/template. Should I wrap two directives with ng-if statements or use one directive with multiple ...

The getStaticProps function in Next.js does not pass any data back to the pages

After setting up my hosted database, I encountered an issue with fetching data from it. Despite confirming that the database is running smoothly through the Swagger app, no data appears when called via API form. import React from 'react'; export ...

Personalizing Google Map pin

I found some code on Codepen to add pointers to a Google map. Currently, it's using default markers but I want to change them to my own. However, I'm not sure where in the code to make this update. Any examples or guidance would be appreciated. ...

Can Google Maps be initialized asynchronously without the need for a global callback function?

Currently, I am working on developing a reusable drop-in Module that can load Google Maps asynchronously and return a promise. If you want to take a look at the code I have constructed for this using AngularJS, you can find it here. One issue I have enco ...

Aggregate values through an onclick event using a get request and iterate through each using

I am struggling to accumulate values obtained from a json get request using jQuery. The problem is that the values keep getting replaced instead of being added up in the variable total. Can someone please guide me on how to properly sum up the values with ...

Is there a way to create a diagonal movement for an image by clicking on another HTML element?

<!DOCTYPE html> <html> <head> <meta charset="UTF-9"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(function() { $(document).re ...

What causes the _.sum() function in lodash to not work with objects in Vuejs?

I've encountered an issue where using Vuejs and Lodash in conjunction with a computed property that calculates the sum of a property in a collection results in unexpected behavior. Instead of summing the values, it seems to concatenate the string [obj ...

Tips for maintaining the navigation tab's consistent appearance

Whenever I click a button on the sidebar and the current tab is the second tab, the navigation bar switches to display the first tab. How can I prevent this switch and keep the second tab displayed when clicking a button on the sidebar? The code for this ...

Develop a professional Angular application for deployment

Help! I'm struggling to build my Angular application for production. After running the 'ng build --prod' command, I can't find all my components in the 'dist' folder. Do I need to change or configure something else? I see som ...

Exploring Protractor testing with Bootstrap modals

I'm having an issue testing a bootstrap modal. Here's the scenario: Click on a button to display the modal The modal body has a list of buttons When I click on one of them, it doesn't do anything My Protractor code snippet: element(by. ...

Choose a row in an Angular ngGrid upon loading the page

My question is in relation to this inquiry How can I retrieve selected rows from ng-grid? Check out the plunker sample - http://plnkr.co/edit/DiDitL?p=preview Upon page load, I am looking to have a row pre-selected without relying on 'ngGridEventDa ...

Error: A surprise 'dot' token was found while defining the function

As part of my API development, I'm working on creating a register route. However, I encountered an issue when trying to utilize the User.function - AddUser method from my model file. The error message mentions an unexpected token .. Below is the code ...

Issues encountered with AngularJS directive binding inner content not functioning as expected

I am currently developing a feature toggle directive, which requires a check to be performed in order to display content if the check is successful. I want the directive to replace itself with its content without creating a new child scope (so I'm try ...

Creating a custom AngularJS HTTP interceptor that targets specific URLs

Is there a way to configure an $http interceptor to only respond to specific URL patterns? For example, I want the interceptor to only intercept requests that match "/api/*" and ignore any other requests. ...

Building on the functionality of AngularJS, a directive scope can be established to access and modify

Can a directive accept and utilize a parameter as its scope value? For instance: angular .module('app', []) .controller('CTRL', function($scope) { $scope.some_value = { instance1: { key1: 'value11', ...

Linking a watcher property to control the opacity value of inline styles

Struggling to connect the opacity of a div with the value of a slider. <div class="container" v-bind:style="opacity">test content</div> Despite my efforts, I can't seem to make the binding work correctly. When I check in the developer to ...

What is the best way to halt an animatePNG sequence?

I found a library that allows me to animate PNG images, you can check it out here: https://github.com/pixelmatrix/animatePNG After looking at the source code, I noticed there is a stop() method available. Can someone guide me on how to call this method in ...

Tips for circumventing CORS in an ajax request using various browser extensions or add-ons

I am encountering a cross-origin problem with my WCF service. Despite adding an extension in chrome to bypass CORS, the issue persists. Is there any other extension/add-on/plugin available to test the service locally and bypass the CORS problem? Edit Aft ...

Efficiency boost: Implementing ajax to load content

Let's discuss the best methods for optimizing content loading with ajax. I will outline a few techniques and provide my insights on each one. Loading html directly - This method makes it easy to load content without much additional processing requir ...

ReactJS does not display an image from the local file system

I'm currently facing an issue while trying to load an image in my next.js project. I am using the native tag from react, but the image is not appearing on the page. I have provided the links to my two JavaScript files (styles and component) below. An ...