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

166
Visualizações
How to use children/slot string inside script tag in svelte

There is some way of use the slot content inside the script tag? something like this:

<CustomComponent>I want to use this text inside the script tag of the custom component</CustomComponent>

and inside the custom component:

<script>console.log(ThatOneString)</script><slot/>

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

0

There is no easy way, in theory a slot can contain 'whatever', strings, html, other components, ... and the most common attitude is "Here is place to something, I don't actually care what you put here".

This means that the only way of somehow accessing this is to use a wrapping element: ```html

```

and then use good old javascript

let wrapper
$: content = wrapper?.innerHTML
$: console.log(content)

(the ?. part is because wrapper will be initially undefined)

But this will not work with dynamic content, if you want to log this every time the content changes, you will have to start messing around with a MutationObserver to track the slot.

import { onMount } from 'svelte'
let content
let wrapper
    
$: console.log(content)
    
onMount(() => {
  const obs = new MutationObserver(() => content = wrapper.innerHTML)
  obs.observe(wrapper, { characterData: true, subtree: true })
  return () => obs.disconnect()
})

at which point I would start wondering if using slots was the correct approach :)

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