在 Vue.js 中使用 slot 元素作为承载分发内容的出口 即插槽,可应用于组合组件的场景中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--View层 模板-->
<div id="app">
<phone>
<phone-title slot="phone-title" :title="title"></phone-title>
<phone-items slot="phone-items" v-for="item in phoneItems" :item="item"></phone-items>
</phone>
</div>
<!--导入Vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
Vue.component("phone",{
template:
'<div>\
<slot name="phone-title"></slot>\
<ul>\
<slot name="phone-items"></slot>\
</ul>\
</div>'
});
Vue.component("phone-title",{
props: ['title'],
template:'<div>{{title}}</div>'
});
Vue.component("phone-items",{
props: ['item'],
template: '<li>{{item}}</li>'
});
var vm = new Vue({
el:"#app",
/*Model层 数据*/
data: {
title: "手机列表",
phoneItems: ["诺基亚","华为","OPPO","VIVO"]
}
});
</script>
</body>
</html>