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

142
Views
Mount component dynamically into DOM in Vue.js

I want to create a text highlighting feature in my app. Here is the idea: I am receiving a piece of HTML from an API endpoint and want to relace some elements with fully working Vue components.

I could achieve it with the help of also this article and came up with the following essential lines:

// node is here the current HTML node from API
const textHighlight = new TextHighlightClass({ propsData: { text: node.innerText } });
textHighlight.$mount(document.getElementById(node.id));

The HTML itself was previously injected into a template element like this:

<div v-html="contentFromApi"></div>

The problem is apparently if you $mount the component instance it will not exist as part of the app hierarchy but on it's own. I'm using a Vuex store to handle the logic so I would like to dispatch an action inside of the TextHighlight component. Trying to access this.$store is not possible as the store is unknown to TextHighlight. A solution for this I could find in this answer doing like this:

const textHighlight = new TextHighlightClass({ store, propsData: { text: node.innerText } });

But this seems to be a workaround while the component itself is still outside of the main application. Is there a better way to attach a component dynamically (programmatically) to the hierarchy?

There is also the way of using the render function but I'm not sure how to use it in my case.

about 4 years ago · Juan Pablo Isaza
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!