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

94
Views
Dynamically render injected vue components

I'm currently working on a third party app, using Vue to render components.

The third party application loads the component dynamically after Vue app is intialized and mounted. As a result, I could only see the HTML of the component, but not actually the template that is inteded to be rendered. Meaning, I could see something like <my-component></my-component> when I inspect, but not the template of the myComponent.

Here is an example of the challenge I'm mentioning about https://jsfiddle.net/EmeraldCodeNinja/31jkmzgc/12/. In this example, you will find a button, clicking on which appends vue component to the DOM using JavaScript.

Please suggest a way to make this work, so I can render dynamically added component.

Note: The intention is not to make the JSFiddle work, it is just an example simulating the challenge I'm facing. My intention is to make the dynamically added vue-component to render.

Posting the same HTML and JS from the JSFiddle

HTML

<script src="https://unpkg.com/vue@3.2.37/dist/vue.global.js"></script>
<div id="app">
  <sample-button></sample-button>
  <div id="new-button-wrap"></div>
</div>

<script id="sample-button" type="text/html">
    <button @click="addNew">Add Another Sample Button</button>
</script>

JS

const app = Vue.createApp();

app.component('sample-button', {
    template: '#sample-button',
  setup() {
    const addNew = function() {
      var div = document.createElement('div');
      var sampleBtn = document.createElement('sample-button');
      div.textContent = 'here a button should appear, but instead a tag is appearing'
      div.appendChild(sampleBtn)
        document.querySelector('#new-button-wrap').appendChild(div)
    }
    return {
        addNew
    }
  }
})

app.mount('#app');
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can create a Vue instance in run-time using createApp and mount methods to append it to the DOM.

createApp({
      template: "<button id=`button`>I'm a button!</button>",
}).mount(document.getElementById("button-container"));

Here is the CodeSandbox link

about 4 years ago · Santiago Trujillo 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!