Vue slot的使用范例

// MyComponent.vue这个是主文件
<template>
  <div>
    <h2>匿名插槽</h2>
    <TodoList>
      <template v-slot:default>
        任意内容
        <p>我是匿名插槽内容</p>
      </template>
    </TodoList>
    <h2>具名插槽</h2>
    <TodoList>
      <template v-slot:chenlili>
        <p>陈丽丽</p>
        <p>我是陈丽丽这个插槽要显示的内容</p>
      </template>
    </TodoList>
    <h2>作用域插槽</h2>
    <TodoList>
      <template v-slot:fn="slotProps">
        {{slotProps.user.firstName}}
        {{slotProps.test.map(item => item)}}
      </template>
    </TodoList>
    <h2>给作用域插槽进行解构赋值</h2>
    <TodoList>
      <template v-slot:fn="{user, test}">
        {{user.firstName}}
        {{test.map(item => item)}}
      </template>
    </TodoList>
    <h1>另外一个地方复用</h1>
    <TodoList>
      <template v-slot:default>5241354354</template>
      <template v-slot:chenlili />
    </TodoList>

  </div>
</template>

<script>
  import TodoList from './TodoList'
export default {
  name: 'MyComponent',
  components: {
    TodoList
  }
}
</script>

<style scoped>

</style>

再来看一下TodoList这个插件

// TodoList.vue被MyComponent.vue所引用的组件
<template>
  <div>
    <h4>TodoList</h4>
    <p>
      <slot>我是默认值,插槽不具名的时候,默认显示的内容</slot>
    </p>
    <slot name="chenlili">当传过来的是陈丽丽的时候,默认要显示的内容</slot>
    <slot name="fn" :user="user" :test="test">
      {{user.lastName}}
    </slot>
  </div>
</template>

<script>
export default {
  name: "TodoList",
  data(){
    return {
      user: {
        lastName: 'lili',
        firstName: 'chen'
      },
      test: [ 1,2,3,4 ]
    }
  }
}
</script>

<style scoped>

</style>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值