Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
How to add multiple components dynamically at run time not globally but locally

Hi i have a situation where i need to register multiple components at run time within a file.

Display.vue

<template>
    <div>
       <component v-if="currentComponent" :is="currentComponent">
                 
        </component>
    </div>
</template>
<script>

import Vue from 'vue';

export default {
    data(){
      return{
        currentComponent:null,
      }
    },
    methods:{
        load(e){
              for(let key in e.components){
                   Vue.component(key,e.components[key]);
               }
              this.$nextTick(() =>{
                   this.currentComponent = e.components.container;
              });
        },
    },

   created(){
       document.body.addEventListener('component-ready',this.load, false);
   },

}
</script>

in my above file how i want my components to be loaded something like shown below:

components:{
   container: component Object{},
   header: component Object{},
   body: component Object {},
   footer: component Object {},
}

here is how i'm dispatching event to above file Display.vue

  const event = new Event('component-ready');
  event.components = {
    container: component Object{},
    header: component Object{},
    body: component Object {},
    footer: component Object {},
   };
  document.body.dispatchEvent(event);

Execution sequence:

  1. event dispatch component-ready
  2. eventlisterner in created of file Display.vue will call load method

Problem: in my current approach components are registered globally, that i want to avoid. i want to register all components to Display.vue file only

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

To locally register the components on the fly, you can copy the component definitions into this.$options.components:

export default {
  methods: {
    load(e) {
      this.$options.components = e.components ๐Ÿ‘ˆ

      this.$nextTick(() => {
        this.currentComponent = e.components.container
      })
    },
  },
}

demo

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!