Vue js import components dynamically

vue dynamic component
vue lazy load component
vue async created
vue :is
vue dynamically render component
import vue from 'vue
vue promise
vue component factory

I have the following parent component which has to render a list of dynamic children components:

<template>
  <div>
    <div v-for="(componentName, index) in supportedComponents" :key="index">
      <component v-bind:is="componentName"></component>
    </div>
   </div>
 </template>

<script>
const Component1 = () => import("/components/Component1.vue");
const Component2 = () => import("/components/Component2.vue");
export default {
  name: "parentComponent",
  components: {
    Component1,
    Component2
  },
  props: {
    supportedComponents: {
      type: Array,
      required: true
    }
  }
};
</script>

The supportedComponents property is a list of component names which I want to render in the parent conponent.

In order to use the children components in the parent I have to import them and register them.

But the only way to do this is to hard code the import paths of the components:

const Component1 = () => import("/components/Component1.vue");
const Component2 = () => import("/components/Component2.vue");

And then register them like this:

components: {
  Component1,
  Component2
}

I want to keep my parentComponent as generic as possible. This means I have to find a way to avoid hard coded components paths on import statements and registering. I want to inject into the parentComponent what children components it should import and render.

Is this possible in Vue? If yes, then how?

You can register components in Vue, and then just use them globally without import or registration.

yourMain.js:

const Component1 = () => import("/components/Component1.vue");
const Component2 = () => import("/components/Component1.vue");


Vue.component('Component1', Component1);
Vue.component('Component2', Component1);

YourComponent.vue

<template>
 <div> 
    <Component1 />
    <Component2 />
 </div>
</template>

https://vuejs.org/v2/guide/components-registration.html

Dynamic & Async Components, Earlier, we used the is attribute to switch between components in a tabbed Vue​.component( 'async-webpack-example', // A dynamic import returns a Promise. One of the really cool features of Vue js is to dynamically load components. Let’s look at some of the examples of components. Components are basically reusable code that can be used across multiple pages. If you try to add the same code for multiple pages then it becomes an absolute mess.

Resolving dynamic webpack import() at runtime

You can dynamically set the path of your import() function to load different components depending on component state.

<template>
  <component :is="myComponent" />
</template>

<script>
export default {
  props: {
    component: String,
  },

  data() {
    return {
      myComponent: '',
    };
  },

  computed: {
    loader() {
      return () => import(`../components/${this.component}`);
    },
  },

  created() {
    this.loader().then(res => {
      // components can be defined as a function that returns a promise;
      this.myComponent = () => this.loader();
    },
  },
}
</script>

Note: JavaScript is compiled by your browser right before it runs. This has nothing to do with how webpack imports are resolved.

Import component dynamically - Get Help, Hi, I've been trying to figure out the best way to import & render components dynamically but haven't managed to make it work. My problem is  You can also return a Promise in the factory function, so with Webpack 2 and ES2015 syntax you can make use of dynamic imports: Vue.component( 'async-webpack-example', // A dynamic import returns a Promise. => import ('./my-async-component') ) When using local registration, you can also directly provide a function that returns a Promise:

You can load the components inside the created lifecycle and register them according to your array property:

<template>
    <div>
        <div v-for="(componentName, index) in supportedComponents" :key="index">
            <component :is="componentName"></component>
        </div>
    </div>
</template>

<script>
    export default {
        name: "parentComponent",
        components: {},
        props: {
            supportedComponents: {
                type: Array,
                required: true
            }
        },
        created ()  {
            for(let c=0; c<this.supportedComponents.length; c++) {
                let componentName = this.supportedComponents[c];
                this.$options.components[componentName] = () => import('./' + componentName + '.vue');
            }
        }
    };
</script>

Works pretty well

Dynamic Imports in Vue.js for better performance, In component importing; In Vue Router, for components mapping; In Vuex modules. Let's take a look at the syntax and focus on the import part. I am trying to import a component dynamically, but all my component and vuex variables seem to be undefined. function loadChart(chartName){ let chart = chartName + '.vue' return System.im

How to make your components dynamic in Vue.js, Before you can create dynamic components in Vue.js, you will need the If you want to mount only one component and then dynamically switch to the v-bind:is​="component" /> </div> </template> <script> import Test from '. This post serves as an introduction to the ways you can reference HTML elements in your components in Vue.js. You can toggle between views or component templates by using the Vue router or by creating dynamic components. The Vue router is used to navigate between views or component templates in the DOM. To use the Vue router, define routes in a routes component and indicate to Vue that the new component should be mounted on an event, such as click.

Dynamic components using VueJS - DEV, Since VueJS will handle all the dirty details of handling DOM, we can focus on also add some components from other files, using ES6 import or required. The strategy was to use names to load the component dynamically  Listen to lifecycle hooks on third-party Vue.js components 11. Simple and performant functional Vue.js components 12. Run watchers when a Vue.js component is created 13. Redirect 404 Not Found in Nuxt.js 14. Testing logic inside a Vue.js watcher 15. Dynamic Imports in Vue.js for better performance 16. Lazy load images with v-lazy-image 17.

Using VueJS computed properties for dynamic module imports, But more importantly, is the fact the VueJS allows you to dynamically import modules and create components using computed properties. And, of course, you can render components passed in props or anything else accessible from the Vue instance. Keep-alive. Right now, any component rendered with <component> will be destroyed entirely when a different component is rendered in its place, and re-created if it is re-added.

Comments
  • Could you pass the component/s as a prop? But somewhere you have to statically import the component. import() is resolved during compilation time, so you need to have it during compilation time.
  • This is not answering the question
  • I get Failed to resolve async component..., Reason: Error: Cannot find module '@/components/General/TextLink.vue'. So it cannot find the module, but that is the correct path.
  • Does the import work without my script? if not, its maybe an other issue.