Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda