美文网首页
Vue插槽的使用

Vue插槽的使用

作者: 等级7 | 来源:发表于2022-07-04 16:25 被阅读0次

    1.匿名slot
    就是在父组件插入的子组件中写入html及css,子组件判断显不显示
    子组件

    <template>
    <slot></slot>
    </template>
    

    父组件

    <template>
    <child>
    <h1>这里有一个匿名插槽</h1>
    </child>
    </template>
    

    2.具名slot
    与匿名插槽效果相同,区别在于加上了name,已经选择那些展示,哪些不
    子组件

    <template>
    <slot name="name"></slot>
    </template>
    

    父组件

    <template>
    <child>
    <h1 slot="name">这里有一个具名插槽</h1>
    </child>
    </template>
    

    3.作用域slot
    带有数据的插槽,使父组件可以调用子组件的数据(不需要$emit,增加子组件复用性)
    子组件

    <template>
    <slot :data="data"></slot>//data是子组件的数据,可以是数组也可以是其他
    </template>
    

    父组件

    <template>
    <child>
    <template slot-scope="user">
    <div>这是一个作用域插槽{{user.data}}</div>//这里父组件接受到了子组件的数据
    </template>
    </child>
    </template>
    

    相关文章

      网友评论

          本文标题:Vue插槽的使用

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