我自己学习插槽查看官方文档也是迷惑了很久,语法知道,但是为什么这么用,就是不明白,后台自己相同了,记个笔记,还是贴下源码比较直观
<div id='app'>
这里是父组件
<child :message='message'>
<template> <div>这个是用来占位置的</div></template>
</child>
</div>
<script>
Vue.component('child',{
props:['message'],
template:'<div>{{message}}<slot></slot></div>'
})
new Vue({
el:'#app',
data:{
message:'这里是子组件'
},
})
</script>
运行结果:
如果在模版中不写<slot></slot>
上方代码运行结果:
所以插槽的作用就是用来占位置的,如果不写<slot></slot>下方模版中的内容就会覆盖<child></child>里边的div 所以这个就是插槽的概念