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

156
Visualizações
How find parent of teleported component in Vue 3?

How can I tell if HTML element that was teleported to body was originnaly in specific component?

Component.vue:

<template>
<div class="parent">
    <p>This is parent</p>

    <teleport>
        <p>This is teleported content</p>
    </teleport>
</div>
</template>

Document:

<body>
    <div id="app">
        <div class="parent">
            <p>
                This is parent
            </p>
        </div>
    </div>

    <p class="teleported">This is teleported</p>
</body>

So now in my code I need to somehow detect that p.teleported is actually inside of Component.vue

What I'm trying to achieve is to detect clicks outside of such components. Existing solutions doesn't work because my components can be nested and teleported multiple times and I have to trace all of them.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you need to have a unique tracking for each teleport, you could use data-attribute and set a counter or something for it.

In case you only need to know the name of the parent, you could do set a common attribute or a classname to it.

Component.vue:

<template>
<div class="parent">
    <p>This is parent</p>

    <teleport to="body">
        <p :data-target=`parentName-${counter}`>This is teleported content</p>
    </teleport>
</div>
</template>
<template>
<div class="parent">
    <p>This is parent</p>

    <teleport to="body">
        <p data-target="parentName">This is teleported content</p>
        <!-- Or you could simply set a classname to it
        <p class="parentName">This is teleported content</p> -->
    </teleport>
</div>
</template>

Now you can access these by document.querySelectorAll("[data-target]") and add onclick events to them in the file where you want the magic to happen.

Hope this could be helpful

https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

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