8 - Vue - slot插槽

6 篇文章 0 订阅
5 篇文章 0 订阅

创建插槽文件
在自动注册组件下创建MySlotss/Iindex,通过props来传值

<template>
    <div>
        <slot name="h3">h3</slot>
        <slot name="h1">h1</slot>//用name接收#名字来排序
        <slot name="h2">h2</slot>
    </div>
</template>

<script>
export default {
    
}
</script>

使用插槽

<template>
  <div id="app">
 <MySlotsIindex>
  <template #h1>//通过#名字传给插槽
    <h1>1</h1>
  </template>

   <template #h2>
    <h2>2</h2>
  </template>

   <template  #h3>
    <h3>3</h3>
  </template>
  </MySlotsIindex>
  </div>
</template>

<script>
export default {
};
</script>

使用插槽的props,

<template>
  <div id="app">
 <MySlotsIindex>
  <template #1="props">
    <h1>count:{{ count }}</h1>//这里的count为props传来的
    <button @click="props.add">+</button>//用props.add,传来的方法
  </template>

解构props
   <template #default="{ count,add }">
    <h2>{{count}}</h2>
    <button @click="add">+</button>
  </template>
<script>
export default {
};
</script>

插槽的props

<slot :a="a" :count="count" name="h2" :add="add"></slot>
<script>
data(){
	return { count:0,a:10};
},
methods:{
	add(){
	this.count++
	}
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值