vue插槽
Vue 实现了一套内容分发的 API,将 <slot> 元素作为承载分发内容的出口,作者称其为插槽,可以应用在组合组件的场景中。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--view层 模板-->
<div id="app">
<p>列表书籍</p>
<ul>
<li>语文</li>
<li>数学</li>
<li>英语</li>
</ul>
</div>
<!--导入vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"></script>
<script>
var vm=new Vue({
el:"#app",
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--view层 模板-->
<div id="app">
<todo>
<todo-title></todo-title>
<todo-items></todo-items>
</todo>
</div>
<!--导入vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"></script>
<script>
//slot插槽
Vue.component("todo",{
template:'<div>\
<slot></slot>\
<ul>\
<slot></slot>\
</ul>\
</div>'
});
Vue.component("todo-title",{
template:'<div>标题</div>'//标题应该是从数据当中进行拿出来的,所以会出现如下的代码
});
Vue.component("todo-items",{
template:'<li>语文</li>' +
'<li>数学</li>'
});
var vm=new Vue({
el:"#app",
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--view层 模板-->
<div id="app">
<todo>
<!--v-bind动态绑定,所以运行之后将直接显示出data中title的内容-秦老师列表-->
<todo-title slot="todo-title" :title="title"></todo-title>
<todo-items></todo-items>
</todo>
</div>
<!--导入vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"></script>
<script>
//slot插槽
Vue.component("todo",{
template:'<div>\
<slot name="todo-title"></slot>\
<ul>\
<slot name="todo-items"></slot>\
</ul>\
</div>'
});
Vue.component("todo-title",{
props:['title'],
template:'<div>{{title}}</div>'
});
Vue.component("todo-items",{
props:['item'],
template:'<li>{{item}}</li>'
});//之后数据就可以变成动态的了,但是数据从哪里来呢,数据应该从data中来,于是来丰富data
var vm=new Vue({
el:"#app",
data:{
title:"秦老师列表",
todoItems:['java','linux','c']
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--view层 模板-->
<div id="app">
<todo>
<!--v-bind动态绑定,所以运行之后将直接显示出data中title的内容-秦老师列表
todoItems中的每一项值是item-->
<todo-title slot="todo-title" :title="title"></todo-title>
<todo-items slot="todo-items" v-for="item in todoItems" :item="item"></todo-items>
</todo>
</div>
<!--导入vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"></script>
<script>
//slot插槽
Vue.component("todo",{
template:'<div>\
<slot name="todo-title"></slot>\
<ul>\
<slot name="todo-items"></slot>\
</ul>\
</div>'
});
Vue.component("todo-title",{
props:['title'],
template:'<div>{{title}}</div>'
});
Vue.component("todo-items",{
props:['item'],
template:'<li>{{item}}</li>'
});//之后数据就可以变成动态的了,但是数据从哪里来呢,数据应该从data中来,于是来丰富data
var vm=new Vue({
el:"#app",
data:{
title:"秦老师列表",
todoItems:['java','linux','c']
}
});
</script>
</body>
</html>