1.插槽
普通插槽,具名插槽,作用域插槽
插槽允许我们在调用子组件的时候为子组件传递模板。
<slot> 元素作为承载分发内容的出口。 一个不带 name 的 <slot> 出口会带有隐含的名字“default”。
父级模板里的所有内容都是在父级作用域中编译的;子模板里的所有内容都是在子作用域中编译的。
1.默认插槽:没有名字的插槽
<slot>123</slot>
<slot name="default">123</slot>
2.具名插槽:具有名字的插槽
<slot name="slot2">123</slot>
3.插槽填充
<my-com>hello</my-com>
简单使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>vue-slot</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.10"></script>
</head>
<body>
<div id="app">
<my-a :arr="arr">
<div>我是子组件</div>
<template v-slot:header> 开始的内容</template>
<template v-slot:center> 中间的内容</template>
<template v-slot:footer> 结束的内容</template>
<template slot-scope="scope"> {
{scope.row}}</template>
</my-a>
</div>
<script>
let myA = {
props: ['arr'],
data() {
return {
msg: '222'
}
},
template: `
<div>
A组件
<slot name='default'></slot>
<div class="header"><slot name='header'></slot></div>
<div class="center"