vue 组件内部如何判断是否提供了slot

不管有没有提供slot,this.$slots都是空对象

<template>
  <div class="smart-item">
    <label class="label">{{label}}: </label>
    <!--判断如果有slot则不显示-->
    <span class="value">{{value}}</span>
    <slot :value="value"></slot>
  </div>
</template>

<script>
  export default {
    data () {
      return {}
    },
    props: {
      label: {
        type: String,
        default: ''
      },
      value: {
        default: ''
      }
    },
    methods: {}
  }
</script>



//使用
 <smart-item label="123" :value="value1"></smart-item>
      <smart-item label="123" :value="value2">
        <template slot-scope="{value}">
          <span class="green">{{value}}</span>
        </template>
      </smart-item>
阅读 15.9k
1 个回答

你这是个作用域插槽,要用this.$scopedSlots
可以看下文档scopedSlots

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题