Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue- Import vue components dynamically

Tags:

I have many components, i want to import then on demand. i have a drop down which actually contains components list, that what to load. I tried this example

<component :is="componentLoader"></component> 

in script

componentLoader () {   return () => import('@/components/testDynamic')  } 

testDynamic is a component name(For now i am trying with static component).

Getting this error

GET http://localhost:8080/0.js net::ERR_ABORTED 404 [Vue warn]: Failed to resolve async component: function () {     return __webpack_require__.e/* import() */(0).then(__webpack_require__.bind(null, "./src/components/testDynamic.vue"));   }   Reason: Error: Loading chunk 0 failed. 

How to fix this? am i doing any thing wrong? or is there any other way to import components dynamically?

like image 970
Sam Avatar asked Nov 21 '18 06:11

Sam


People also ask

How do I import Vue components?

STEP 01: First, Import the Child Component into the Parent Component inside script tag but above export default function declaration. STEP 02: Then, Register the Child Component inside the Parent Component by adding it to components object. STEP 03: Finally, Use the Child Component in the Parent Component Template.

How do I create a dynamic component in Vue 3?

To create a dynamic component, we use the component element and bind the is prop to it to specify which component we want to render. note The component and slot elements aren't true components, they are component-like features of Vue's template syntax. We do not have to import them like we do with regular components.

Is Vue synchronous or asynchronous?

You might know Vue updates reactively: when you change a value, the DOM is automatically updated to reflect the latest value. Vue does these updates asynchronously. In contrast, a test runner like Jest runs synchronously. This can cause some surprising results in tests.


1 Answers

You can register async dynamic components locally in a single file component like this:

export default {   components: {     'test-dynamic': () => import('@/components/testDynamic'),     'other-dynamic': () => import('@/components/otherDynamic')   },   data () {     return {       current: 'test-dynamic'     }   } } 

And in your template:

<component :is="current"></component> 

If you register multiple components then you would just change the value of current to the desired component.

In the case of many components, you can import an object mapping the component names to their respective file paths, then register them like:

import myComponents from '@/components'  export default {   components: Object.keys(myComponents).reduce((obj, name) => {     return Object.assign(obj, { [name]: () => import(myComponents[name]) })   }, {})   ... } 

Where myComponents is exported as:

// components/index.js export default {   foo: '@/path/to/Foo',   bar: '@/path/to/Bar',   ... } 
like image 77
Brian Lee Avatar answered Sep 20 '22 19:09

Brian Lee