美文网首页
Vue3 通过JS获取插槽内容

Vue3 通过JS获取插槽内容

作者: 曾祥辉 | 来源:发表于2021-04-09 00:46 被阅读0次

先上样例

//父组件
<template>
  <div>Tabs示例
    <h1>示例1</h1>
    <Tabs v-model:selected="x">
      <Tab title="导航1">内容1</Tab>
      <Tab title="导航2">内容2</Tab>
    </Tabs>
  </div>
</template>

//子组件
......
<script lang="ts">
export default {
    setup(props, context) {
      //获得插槽虚拟节点<数组>
       const defaults = context.slots.default();
    }
}
</script>
......

注:setup下context.slots.default()获取的虚拟节点可以跟import引用子组件比较

//子组件
......
<script lang="ts">
import Component  from "./Component.vue";
export default {
    setup(props, context) {
      //获得插槽虚拟节点<数组>
       const defaults = context.slots.default();
        if(defaults [0].type === Component){
          // coding
        }
    }
}
</script>
......

相关文章

网友评论

      本文标题:Vue3 通过JS获取插槽内容

      本文链接:https://www.haomeiwen.com/subject/mkkbkltx.html