1、为什么使用插槽
组件的插槽是为了让组件更具有扩展性
2、如何封装这类组件
3、插槽的基本使用
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!--
1、插槽的基本使用<slot></slot>
2、插槽的默认值<slot>button</slot>
3、如果有多个值,同时插入到组件进行替换时,这些值会一起作为替换元素
-->
<div id="app">
<cpn></cpn>
<cpn>
<p>哈哈哈哈</p>
<span>嘿嘿</span>
<p>我是按钮</p>
</cpn>
<cpn><p>hhhhhhhh</p></cpn>
<cpn></cpn>
<cpn></cpn>
<cpn></cpn>
</div>
<template id="cpn">
<div>
<h1>标题</h1>
<h1>内容</h1>
<slot><button>我是按钮</button></slot>
</div>
</template>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message:'你好'
},
methods: {
},
components:{
cpn: {
template: '#cpn'
}
}
})
</script>
</body>
</html>
4、具名插槽的使用
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<cpn><span slot="left">左标题</span></cpn>
<cpn><button slot="right">返回</button></cpn>
</div>
<template id="cpn">
<div>
<slot name="left"><span>左边</span></slot>
<slot name="center"><span>中间</span></slot>
<slot name="right"><span>右边</span></slot>
</div>
</template>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message:'你好'
},
methods: {
},
components:{
cpn: {
template: '#cpn'
}
}
})
</script>
</body>
</html>
5、编译作用域的概念
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<cpn v-show= "isShow"></cpn>
</div>
<template id="cpn">
<div>
<h2>标题</h2>
</div>
</template>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message:'你好',
isShow: true
},
methods: {
},
components:{
cpn: {
template: '#cpn',
data(){
return{
isShow:false
}
}
},
}
})
</script>
</body>
</html>
6、作用域插槽的使用
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<cpn></cpn>
<cpn>
<!-- 目的是获取子组件中的planguages -->
<template slot-scope="slot">
<span>{{slot.data.join(' * ')}}</span>
</template>
</cpn>
<cpn>
<!-- 目的是获取子组件中的planguages -->
<template slot-scope="slot">
<span>{{slot.data.join(' - ')}}</span>
</template>
</cpn>
</div>
<template id="cpn">
<slot :data="pLanguages">
<ul>
<li v-for="item in pLanguages">{{item}}</li>
</ul>
</slot>
</template>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message:'你好',
},
methods: {
},
components:{
cpn: {
template: '#cpn',
data(){
return{
pLanguages:['Java','Python','Swift','Go','c++','c#','JaaScript']
}
}
},
}
})
</script>
</body>
</html>