vue插槽

插槽,名词理解:在组件嵌套标签中使用,在原组件上放置标识标签(通俗来说就是挖坑标记标识);在使用组件上通过属性将实际与标识标签连接,指引实际标签放置在原组件的相对位置(通俗来说就是在标识位置填坑)

用法:

原组件:

<div class="container">

<header>

<slot name="header"></slot>  //<slot name="header"></slot>为标识标签,说明header的标准放置在这里

</header>

<main>

<slot name="main"></slot> //<slot name="main"></slot>为标识标签,说明header的标准放置在这里

</main>

<footer>

<slot name="footer"></slot>  //<slot name="footer"></slot>为标识标签,说明header的标准放置在这里

</footer> </div>

使用组件:

<base-layout>  //<base-layout>为组件名(自定义的)

<template slot="header">  //slot="header",将template 标签所包裹的标签放置到原组件<slot name="header"></slot>的位置上

<h1>Here might be a page title</h1>

</template>

<p slot="main">A paragraph for the main content.</p>

<p slot="main">And another one.</p>   //slot="main",将template 标签所包裹的标签放置到原组件<slot name="main"></slot>的位置上

<template slot="footer">   //slot="footer",将template 标签所包裹的标签放置到原组件<slot name="footer"></slot>的位置上

<p>Here's some contact info</p>

</template>

</base-layout>

补充:

1.当使用<template></template>包裹标签时。可使用 v-slot:x(名字)代替slot="x"(名字)如下:

<template  v-slot:x>

········

</template>

2.插槽数据传递,原组件传数据给组件使用者

在原组件上添加属性如:

<slot name="main" :x="y"></slot>  //x为数据标识名,y为数据或者变量。

在组件使用者上添加属性如下:

<template scope="X">

········

</template>  

注意:1.X(X为自定义名字);2.该用法只能在<template></template>  上使用;3.数据使用方式为:X.x(x为数据标识名)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值