详解Vue中插槽(slot)与具名插槽的使用
本文详细介绍Vue中插槽(slot)的基本用法、默认值机制以及具名插槽(named slots)的实现方式。通过子组件定义与父组件引用的代码示例,解析如何控制插槽内容的显示,并解决多个插槽同时存在时的命名冲突问题,帮助开发者掌握Vue组件内容分发的核心技巧。
基本插槽与默认值机制
一、基本的插槽
这里总结两点
- 如果不在子组件中使用插槽(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.js 是什么:渐进式 JavaScript 框架的核心优势与适用场景
2026-11-03 20:00:13
为什么JS代码执行了但页面没有跳转
2026-11-03 16:29:09
Vue.js 项目中 Axios 拦截器封装与接口对接实战
2026-11-02 10:22:43
前端PC端自适应开发:两种常见适配方案解析
2026-11-01 03:30:54
Vue3中通过JS全局调用弹窗组件的实现方案
2026-10-31 21:34:31








