详解Vue中使用插槽(slot)、聚类插槽
2019-04-12 08:35
821 查看
一、基本的插槽
这里总结两点
- 如果不在子组件中使用插槽(slot),那么在子组件中写任何代码都是无效的的,不会显示
- (插槽默认值)如果子组件中没有插入任何代码的话就会显示组件插槽中的内容
slot 代表父组件往子组件中 插入的标签
这里就代表组件子组件中的
<p>Dell</p> <child> <p>Dell</p> </child>
这里如果是这样的
<child> </child>
就会显示 <slot>默认内容</slot>中的默认内容
二、聚类插槽
1、如果不在子组件中使用插槽(slot),那么在子组件中写任何代码都是无效的的,不会显示
2、(插槽默认值)如果子组件中没有插入任何代码的话就会显示组件插槽中的内容
这里如果是这样的
<child> </child>
就会显示<slot>默认内容</slot>中的 默认内容
3、聚类插槽
子组件这么写:
template:`<div> <slot>默认内容</slot> <p>content</p> <slot>默认内容</slot> </div>
然后这么引用:
<child> <div>header</div> <div>footer</div> </child>
就会发现结果是
header
footer
content
header
footer
这个不是我的本意,那么怎么办,这里就引入了聚类插槽
子组件:
template:`<div> <slot name='header'>默认内容</slot> <p>content</p> <slot name='footer'>默认内容</slot> </div>`
子组件引用:
<child> <div slot='header'>header</div> <div slot='footer'>footer</div> </child>
不难发现给每个想要指定的子组件插槽添加 name属性,然后在引用中 slot中明确 是哪个即可也可以理解为引用中是用了两个插槽同时,默认内容同时适用在每个插槽
三、作用域插槽
这个是普通插槽的Demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue中使用插槽(slot)</title> <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script> </head> <body> <div id="root"> <!-- 1、如果不在子组件中使用插槽(slot),那么在子组件中写任何代码都是无效的的,不会显示 2、(插槽默认值)如果子组件中没有插入任何代码的话就会显示组件插槽中的内容 这里如果是这样的 <child> </child> 就会显示 <slot>默认内容</slot>中的 默认内容 --> <child> <p>Dell</p> </child> </div> <script type="text/javascript"> Vue.component('child',{ /* slot 代表 父组件往子组件中 插入的标签 这里就代表 组件子组件中的 <p>Dell</p> <child> <p>Dell</p> </child> */ template:`<div> <slot>默认内容</slot> </div>` }); var vm = new Vue({ el:'#root', }); </script> </body> </html>
这个是聚类插槽的Demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue中使用插槽(slot)</title> <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script> </head> <body> <div id="root"> <!-- 1、如果不在子组件中使用插槽(slot),那么在子组件中写任何代码都是无效的的,不会显示 2、(插槽默认值)如果子组件中没有插入任何代码的话就会显示组件插槽中的内容 这里如果是这样的 <child> </child> 就会显示 <slot>默认内容</slot>中的 默认内容 3、聚类插槽 子组件这么写:template:`<div> <slot>默认内容</slot> <p>content</p> <slot>默认内容</slot> </div>` 然后这么引用:<child> <div>header</div> <div>footer</div> </child> 就会发现结果是 header footer content header footer 这个不是我的本意,那么怎么办,这里就引入了聚类插槽 子组件:template:`<div> <slot name='header'>默认内容</slot> <p>content</p> <slot name='footer'>默认内容</slot> </div>` 子组件引用:<child> <div slot='header'>header</div> <div slot='footer'>footer</div> </child> 不难发现给每个想要指定的子组件插槽添加 name属性, 然后在引用中 slot中明确 是哪个即可 也可以理解为引用中是用了两个插槽 同时,默认内容同时适用在每个插槽 --> <child> <div slot='header'>default header</div> <div slot='footer'>default footer</div> </child> </div> <script type="text/javascript"> Vue.component('child',{ /* slot 代表 父组件往子组件中 插入的标签 这里就代表 组件子组件中的 <p>Dell</p> <child> <p>Dell</p> </child> */template:`<div> <slot name='header'>默认内容</slot> <p>content</p> <slot name='footer'>默认内容</slot> </div>` }); var vm = new Vue({ el:'#root', }); </script> </body> </html>
以上所述是小编给大家介绍的Vue中使用插槽(slot)、聚类插槽详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:
相关文章推荐
- 详解vue slot插槽的使用方法
- 详解vue使用插槽分发内容slot的用法
- 使用Vue的slot插槽分发父组件内容实现高度复用、更加灵活的组件(推荐)
- vue插槽slot的理解与使用
- vue slot插槽的使用方法
- vue组件详解之使用slot分发内容
- vue使用插槽分发内容slot的用法
- Vue中的slot使用插槽分发内容的方法
- 详解Vue 匿名、具名和作用域插槽的使用方法
- 使用Vue的slot插槽分发父组件内容实现高度复用、更加灵活的组件
- vue如何使用 Slot 分发内容实例详解
- 详解Vue组件插槽的使用以及调用组件内的方法
- 深入解析vue使用插槽分发内容slot的用法
- vue插槽使用与具名插槽使用(slot)
- vue组件详解(四)——使用slot分发内容
- 详解vue2.6插槽更新v-slot用法总结
- Vue.js学习之使用Webpack加速应用的方式详解
- 使用slot分发内容 作用域插槽
- vue2.0之axios使用详解(一)
- 详解在Vue中有条件地使用CSS类