版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
一.具名插槽
<!DOCTYPE html>
<html lang="en">
<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>插槽slot</title>
<script src="./js/vue.js"></script>
</head>
<body>
<div id="root">
<body-content>
<!-- slot="插槽名" 定义插槽名 -->
<div class="header" slot="header">I am header</div>
<div class="footer" slot="footer">I am footer</div>
</body-content>
</div>
<script>
Vue.component('body-content', {
// 通过<slot name="">使用具名插槽
template:`<div>
<slot name="header"></slot>
<div class="content">content</div>
<slot name="footer">
<em>defalut ftext</em>
</slot>
</div>`
})
var vm = new Vue({
el: '#root',
})
</script>
</body>
</html>
二.作用域插槽
<!DOCTYPE html>
<html lang="en">
<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>
<script src="./js/vue.js"></script>
</head>
<body>
<div id="root">
<child>
<template slot-scope="props">
<li>{{props.item}}</li>
</template>
</child>
</div>
<script>
Vue.component('child', {
data: function(){
return {
list: [1,2,3,4]
}
},
template: `<div>
<ul>
<slot v-for="item of list"
:item=item
></slot>
</ul>
</div>`
})
var vm = new Vue({
el: '#root',
})
</script>
</body>
</html>