vue之slot插槽

普通用法:

1. 新建compA.vue组件,内容为:

<template>
	<div>
		<div class="head">
			<slot name="head">具名插槽-头</slot>
		</div>
		<slot>占位内容</slot>
		<div class="foot">
			<slot name="foot">具名插槽-尾</slot>
		</div>
	</div>
</template>

2.在test.vue文件中导入并使用:

<template>
	<div>
        <compA></compA>
        <compA>只替换正文内容</compA>
		<compA>
			<template slot="head">真正要的头部</template>
			其他正文内容...
			<template slot="foot">真正要的尾部</template>
		</compA>
	</div>
</template>

<script>
import compA from "./compA";
export default {
    components:{compA},
}
</script>

<style>
</style>



3.页面展示结果:

<slot name="head"> : slot标签设置插槽,name属性设置插槽名称

<template slot="head"> : slot属性设置使用的插槽,vue2.6以后的版本会用v-slot来替代

高级用法:(使用的是vue版本至少要是2.6.0)

1.组件设置两个插槽,一个默认的<slot></slot>,一个<slot name="option" :item="item"></slot>并且绑定了参数item

<el-select :value.sync="value" :placeholder="placeholder" :clearable="clearable" :disabled="disabled"  @change="change">
    <slot></slot>
    <el-option :label="item.name" :value="item.id" v-for="item in datas" :key="item.id">
      <slot name="option" :item="item"></slot>
    </el-option>
  </el-select>

2.<el-option label="123" value="1"></el-option>是默认插槽的节点,

<template v-slot:option='scope'>是绑定了具名插槽option的节点,并且通过v-slot接收到参数scope,

<SelectRemote v-model="form.financeDivideId" @change="d=>form.financeDivideId=d" url="/financeDivide/all" placeholder="请选择资金账号" :required="true">
  <el-option label="123" value="1"></el-option>
  <template v-slot:option='scope'>
      <span style="float: left">{{ scope.item.id }}</span>
      <span style="float: right;font-size: 13px">名称:{{ scope.item.name }}</span>
  </template>
</SelectRemote>

也可以通过结构赋值 v-slot:option='{item}'直接拿到绑定的参数,如下:

<SelectRemote v-model="form.financeDivideId" @change="d=>form.financeDivideId=d" url="/financeDivide/all" placeholder="请选择资金账号" :required="true">
  <el-option label="123" value="1"></el-option>
  <template v-slot:option='{item}'>
      <span style="float: left">{{ item.id }}</span>
      <span style="float: right;font-size: 13px">名称:{{ item.name }}</span>
  </template>
</SelectRemote>

3.v-slot的省略写法 # ,和解构赋值

<div class="container">
  <header>
    <slot name="header" :attr1="data1"></slot>
  </header>
  <main>
    <slot :attr2="data2"></slot>
  </main>
  <footer>
    <slot name="footer" :attr3="data3"></slot>
  </footer>
</div>


<layout>
  <!-- 正常传参 -->
  <template #header="props">
    <h1>{{props.attr1}}</h1>
  </template>

  <!-- 解构赋值 -->
  <template #default="{attr2}">
    <p>{{attr2}}</p>
  </template>

  <!-- 解构赋值并重命名 -->
  <template #footer="{attr3:myProp}">
    <p>{{myProp}}</p>
  </template>
</layout>

<slot>标签的name属性给插槽设置名称,通过(v-bild或者:)绑定的自定义属性用于传参

v-slot 只能添加在 <template> 上, v-slot可以使用缩写形式 # ,比如v-slot:header="xxx" 等价于 #header="xxx" ,使用缩写必须要有名称, 默认的使用 #default

参考:官方文档

老版本传参的参考:官方文档

Vue中的插槽slot)是一种用于在组件中插入内容的机制。它允许我们在组件的模板中定义一些占位符,然后在使用该组件时,可以将具体的内容插入到这些占位符中。 在Vue 2.x版本中,我们可以使用`<slot>`元素来定义插槽,并使用`<slot>`元素的`name`属性来定义具名插槽。例如: ```html <template> <div> <slot></slot> <!-- 默认插槽 --> <slot name="header"></slot> <!-- 具名插槽 --> <slot name="footer"></slot> <!-- 具名插槽 --> </div> </template> ``` 然后,在使用该组件时,我们可以在组件标签中插入内容,这些内容将会替换对应的插槽。例如: ```html <my-component> <p>默认插槽的内容</p> <template v-slot:header> <h1>具名插槽header的内容</h1> </template> <template v-slot:footer> <p>具名插槽footer的内容</p> </template> </my-component> ``` 在Vue 3.x版本中,为了统一插槽的语法,引入了`v-slot`指令,取代了`slot`和`slot-scope`这两个在Vue 2.x中已被废弃但仍可使用的属性。使用`v-slot`指令时,可以直接在组件标签上使用,而不需要再使用`<template>`元素。例如: ```html <my-component> <template #default> <p>默认插槽的内容</p> </template> <template #header> <h1>具名插槽header的内容</h1> </template> <template #footer> <p>具名插槽footer的内容</p> </template> </my-component> ``` 需要注意的是,在Vue 3.x中,只能使用`v-slot`指令来定义插槽,而不能再使用`slot`和`slot-scope`属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值