vue中怎么使用slot分发内容(slot,vue,web开发)

时间:2024-05-02 10:47:12 作者 : 石家庄SEO 分类 : web开发
  • TAG :

一、什么是slot

在使用组件时,我们常常要像这样组合它们:

<app><app-header></app-header><app-footer></app-footer></app>

当需要让组件组合使用,混合父组件的内容与子组件的模板时,就会用到slot , 这个过程叫作内容分发( transclusion )。

注意两点:

1.< app>组件不知道它的挂载点会有什么内容。挂载点的内容是由<app >的父组件决定的。

2.<app> 组件很可能有它自己的模板。

props 传递数据、events 触发事件和slot 内容分发就构成了Vue 组件的3 个API 来源,再复杂的组件也是由这3 部分构成的。

二、作用域

<child-component>{{message}}</child-component>

这里的message 就是一个slot ,但是它绑定的是父组件的数据,而不是组件<child-component>的数据。

父组件模板的内容是在父组件作用域内编译,子组件模板的内容是在子组件作用域内编译。如:

<divid="app15"><child-componentv-show="showChild"></child-component></div>Vue.component('child-component',{template:'<div>子组件</div>'});varapp15=newVue({el:'#app15',data:{showChild:true}});

这里的状态showChild 绑定的是父组件的数据,如果想在子组件上绑定,那应该是:

<divid="app15"><child-component></child-component></div>Vue.component('child-component',{template:'<divv-show="showChild">子组件</div>',data:function(){return{showChild:true}}});

因此, slot 分发的内容,作用域是在父组件上的。

三、slot用法

3.1 单个slot

在子组件内使用特殊的<slot>元素就可以为这个子组件开启一个slot(插槽),在父组件模板里,插入在子组件标签内的所有内容将替代子组件的<slot> 标签及它的内容。

<divid="app16"><my-component16><p>分发的内容</p><p>更多分发的内容</p></my-component16></div>Vue.component('my-component16',{template:'<div>'+'<slot><p>如果父组件没有插入内容,我将作为默认出现<</p></slot>'+    //预留的slot插槽'</div>'});varapp16=newVue({el:'#app16'});

渲染结果为:

<divid=”app16”><div><p>分发的内容<p><p>更多分发的内容<p></div></div>

子组件child-component 的模板内定义了一个<slot>元素,并且用一个<p>作为默认的内容,在父组件没有使用slot 时,会渲染这段默认的文本;如果写入了slot, 那就会替换整个<slot> 。

3.2具名slot

给<slot> 元素指定一个name 后可以分发多个内容,具名Slot 可以与单个slot 共存。

<divid="app17"><my-component17><h4slot="header">标题</h4><p>正文内容</p><p>更多正文内容</p><h4slot="footer">底部信息</h4></my-component17></div>Vue.component('my-component17',{template:'<divclass="container">'+'<divclass="header">'+'<slotname="header"></slot>'+'</div>'+'<divclass="main">'+'<slot></slot>'+'</div>'+'<divclass="footer">'+'<slotname="footer"></slot>'+'</div>'+'</div>'});varapp17=newVue({el:'#app17'});

渲染结果为:

<divid="app17"><divclass="container"><divclass="header"><h4>标题</h4></div><divclass="main"><p>正文内容</p><p>更多正文内容</p></div><divclass="footer"><h4>底部信息</h4></div></div></div>

子组件内声明了3 个<s lot>元素,其中在<div class=” main >内的<slot> 没有使用name 特性,它将作为默认slot 出现,父组件没有使用slot 特性的元素与内容都将出现在这里。

如果没有指定默认的匿名slot ,父组件内多余的内容片段都将被抛弃。

四、作用域插槽

作用域插槽是一种特殊的slot ,使用一个可以复用的模板替换己渲染元素。

看一个例子:

<divid="app18"><my-component18><templatescope="props"><p>来自父组件的内容</p><p>{{props.msg}}</p></template></my-component18></div>Vue.component('my-component18',{template:'<divclass="container"><slotmsg="来自子组件的内容"></slot></div>'});varapp18=newVue({el:'#app18'});

观察子组件的模板,在<slot>元素上有一个类似props 传递数据给组件的写法msg=” xxx ”,将数据传到了插槽。

父组件中使用了<template>元素,而且拥有一个scope=”props ”的特性,这里的props只是一个临时变量,就像v-for= ” item in items 里面的item 一样,template 内可以通过临时变量props访问来自子组件插槽的数据msg 。

下面看下Vue组件中slot的用法

主要是让组件的可扩展性更强。

1. 使用匿名slot

vue中怎么使用slot分发内容

2. 给slot加个名字

vue中怎么使用slot分发内容

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:vue中怎么使用slot分发内容的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Springboot @Value注入boolean如何设置默认值下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18