N
N
Nikita Ronzin2019-11-21 17:51:02
JavaScript
Nikita Ronzin, 2019-11-21 17:51:02

How can I connect dynamic components?

I want to make a dynamic connection of components depending on the received props. I tried through this guide, but webPack does not receive the passed value. I read the vue documentation, but the dynamics there are due to the child components already connected to the parent. Those. it's a pseudo-dynamic. Perhaps someone faced a similar problem. Building WebPack3 and Vue2.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2019-11-22
@Ababinili

Dynamic import of a component, depending on the incoming props
1. File structure:

AppTest.vue
components/
  Test-1.vue
  Test-2.vue

2. Usage:
<div>
  <app-test component-name="Test-1" />
</div>

3.AppTest.vue
<template>
  <div>
    <component :is="componentName"></component>
  </div>
</template>

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

  created() {
    this.$options.components[this.componentName] = () => import(`./components/${this.componentName}.vue`);
  }
};
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question