简体   繁体   中英

how to pass children in Vue 3 and TSX

everybody.

I have an issue while passing children with Vue 3 SFC and TSX.

<script lang="tsx" setup>
...
const DrugBoxWrap = ({ a, children }) => {
  console.log(a) // b
  console.log(children) // undefined
  return (
    <>
      {children}
    </>
  )
}
</script>
<template>
  <DrugBoxWrap a="b"> Hello </DrugBoxWrap>
</template>

Props a got string of b and it's working well. But why is children undefined?

VueJS does not wrap a default slot in a children prop. You have to explicitly call the slot. Btw I think you should directly create a component in a tsx file and use a render function instead of the setup.

import { defineComponent } from 'vue';

export default defineComponent({
  name: 'DrugBoxWrap',
  render(c) {
    return (
      <>
        {c?.$slots?.default?.()}
      </>
    )
  },
})

I created a repository with few examples, I hope it can help you: https://github.com/shenron/vite-demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM